如何解决当我想从子包和 python 包中生成多个命令行应用程序时,如何编写准确的“setup.py”(entry_points)
我想根据教程写一个小的个人包:https://gehrcke.de/2014/02/distributing-a-python-command-line-application/。 目录结构如下所示:
python-cmdline-bootstrap/
├── docs
├── test
├── bootstrap
│ ├── __init__.py
│ ├── __main__.py
│ ├── bootstrap.py
│ └── stuff.py
├── bootstrap-runner.py
├── LICENSE
├── MANIFEST.in
├── README.rst
└── setup.py
我进一步在 bootstrap fold 下添加了一个新模块 (src
) 和脚本 (case.py
),如下所示:
python-cmdline-bootstrap/
...
├── bootstrap
│ ├── __init__.py
│ ├── __main__.py
│ ├── bootstrap.py
│ ├── stuff.py
│ ├── src
│ ├── __init__.py
│ ├── case.py
├── bootstrap-runner.py
...
case.py
的内容如下:
# -*- coding: utf-8 -*-
import argparse
def case():
print("This a new command.")
我将以下几行添加到 setup.py 中:
console_scripts = """
[console_scripts]
bootstrap = bootstrap.bootstrap:main
cccase = bootstrap.src.case:case
"""
当我安装后在终端中执行python setup.py install
并运行cccase
时,它显示错误:
Traceback (most recent call last):
File "/home/chxp/tmp/python3-test/bin/cccase",line 11,in <module>
load_entry_point('cmdline-bootstrap==0.2.0','console_scripts','cccase')()
File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py",line 489,in load_entry_point
return get_distribution(dist).load_entry_point(group,name)
File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py",line 2852,in load_entry_point
return ep.load()
File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py",line 2443,in load
return self.resolve()
File "/home/chxp/tmp/python3-test/lib/python3.8/site-packages/pkg_resources/__init__.py",line 2449,in resolve
module = __import__(self.module_name,fromlist=['__name__'],level=0)
ModuleNotFoundError: No module named 'bootstrap.src'
如果我使用 ./bootstrap-runner.py
或 python -m bootstrap
效果很好,我认为这可能是 setup.py
中的错误。
因此,我该如何修改 setup.py?我想在一个 python 包中生成不同的命令行应用程序。
与此同时,我如何在一次运行中使用 ./bootstrap-runner.py
或 python -m bootstrap
测试多个命令行应用程序。
看来我需要更改__main__.py
或bootstrap-runner.py
中的内容来测试每个命令行应用程序,例如bootstrap
、cccase
。
原始脚本在“https://gitee.com/chxp/python-cmdline-bootstrap”上传并由git clone https://gitee.com/chxp/python-cmdline-bootstrap.git
下载。
感谢您的帮助。
我在这里看到了解释:Why do I need to include sub-packages in setup.py。它完美地解决了我的问题。
解决方法
我找到了解决方案!如果指定packages
,函数setup
中的packages
参数似乎无法识别子模块。
因此以下更改将起作用:
from setuptools import setup,find_packages
setup(
name="cmdline-bootstrap",packages=find_packages(),entry_points=console_scripts,version=version,description="Python command line application bare bones template.",long_description=long_descr,author="Jan-Philip Gehrcke",author_email="jgehrcke@googlemail.com",url="http://gehrcke.de/2014/02/distributing-a-python-command-line-application",)
只需使用 packages=["bootstrap"]
替换 packages=find_packages()
。
但我仍然不知道如何真正解释问题以及如果我仍然想使用packages=["bootstrap"]
需要做什么。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。