单击模块 Setuptools 示例开箱即用
Click module Setuptools example didn't work out of the box
Click setuptools 有两个例子。我可以让第一个例子起作用,但第二个例子不行。
我知道还有两个文件,但现在 yourscript.py
存储在子目录 scripts
:
# yourscript.py
import click
@click.command()
def cli():
"""Example script."""
click.echo('Hello World!')
Setup.py 然后提供参数 find_packages()
并且 entry_points
使用点语法来描述 yourscript.py
的位置
# setup.py
from setuptools import setup, find_packages
setup(
name='yourpackage',
version='0.1',
packages=find_packages(),
include_package_data=True,
install_requires=[
'Click',
],
entry_points='''
[console_scripts]
yourscript=yourpackage.scripts.yourscript:cli
''',
)
我按照示例使用 virtualenv:
(clickenv) > pip install -e .
Obtaining file:///home/...ClickSiteExample/yourscript
Requirement already satisfied: Click in /home/.../clickenv/lib/python3.6/site-packages (from yourpackage==0.1) (6.7)
Installing collected packages: yourpackage
Running setup.py develop for yourpackage
Successfully installed yourpackage
然后调用脚本:
(clickenv) > yourscript
[...]
ModuleNotFoundError: No module named 'yourpackage'
此错误报告中缺少某些内容,因为软件包也显示为已安装:
(clickenv) [130] > pip list
Package Version Location
----------- ------- --------------------------------------------------
click 6.7
pip 10.0.0
setuptools 39.0.1
wheel 0.31.0
yourpackage 0.1 /home/.../ClickSiteExample/yourpackage <<CORRECTED
这应该很简单,那么示例中缺少什么?
更新。我重新创建了目录,去掉了两个未提及的文件 main.py
和 utils.py
。我知道 __init__.py
文件是必需的,但可以为空,我已经包含了这些文件:
(clickenv) > tree
.
└── yourpackage <<CORRECTED
├── __init__.py
├── scripts
│ ├── __init__.py
│ └── yourscript.py
├── setup.py
└── yourpackage.egg-info
├── dependency_links.txt
├── entry_points.txt
├── PKG-INFO
├── requires.txt
├── SOURCES.txt
└── top_level.txt
这是有效的目录结构:
yourpackage <- Any top-level directory will do
|-- setup.py
|-- yourpackage
| |-- __init__.py
| `-- scripts
| |-- __init__.py
| |-- yourscript.py
pip install -e .
之后:
`-- yourpackage.egg-info
|-- dependency_links.txt
|-- entry_points.txt
|-- PKG-INFO
|-- requires.txt
|-- SOURCES.txt
`-- top_level.txt
入口点脚本有效:
$ yourscript
Hello World!
Click setuptools 有两个例子。我可以让第一个例子起作用,但第二个例子不行。
我知道还有两个文件,但现在 yourscript.py
存储在子目录 scripts
:
# yourscript.py
import click
@click.command()
def cli():
"""Example script."""
click.echo('Hello World!')
Setup.py 然后提供参数 find_packages()
并且 entry_points
使用点语法来描述 yourscript.py
# setup.py
from setuptools import setup, find_packages
setup(
name='yourpackage',
version='0.1',
packages=find_packages(),
include_package_data=True,
install_requires=[
'Click',
],
entry_points='''
[console_scripts]
yourscript=yourpackage.scripts.yourscript:cli
''',
)
我按照示例使用 virtualenv:
(clickenv) > pip install -e .
Obtaining file:///home/...ClickSiteExample/yourscript
Requirement already satisfied: Click in /home/.../clickenv/lib/python3.6/site-packages (from yourpackage==0.1) (6.7)
Installing collected packages: yourpackage
Running setup.py develop for yourpackage
Successfully installed yourpackage
然后调用脚本:
(clickenv) > yourscript
[...]
ModuleNotFoundError: No module named 'yourpackage'
此错误报告中缺少某些内容,因为软件包也显示为已安装:
(clickenv) [130] > pip list
Package Version Location
----------- ------- --------------------------------------------------
click 6.7
pip 10.0.0
setuptools 39.0.1
wheel 0.31.0
yourpackage 0.1 /home/.../ClickSiteExample/yourpackage <<CORRECTED
这应该很简单,那么示例中缺少什么?
更新。我重新创建了目录,去掉了两个未提及的文件 main.py
和 utils.py
。我知道 __init__.py
文件是必需的,但可以为空,我已经包含了这些文件:
(clickenv) > tree
.
└── yourpackage <<CORRECTED
├── __init__.py
├── scripts
│ ├── __init__.py
│ └── yourscript.py
├── setup.py
└── yourpackage.egg-info
├── dependency_links.txt
├── entry_points.txt
├── PKG-INFO
├── requires.txt
├── SOURCES.txt
└── top_level.txt
这是有效的目录结构:
yourpackage <- Any top-level directory will do
|-- setup.py
|-- yourpackage
| |-- __init__.py
| `-- scripts
| |-- __init__.py
| |-- yourscript.py
pip install -e .
之后:
`-- yourpackage.egg-info
|-- dependency_links.txt
|-- entry_points.txt
|-- PKG-INFO
|-- requires.txt
|-- SOURCES.txt
`-- top_level.txt
入口点脚本有效:
$ yourscript
Hello World!