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
答案 0 :(得分:0)
这是有效的目录结构:
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!