如何使用 setuptools 安装 python cli 脚本而不重复?
我有 2 个可以从 shell 中使用的 Python 脚本,因为它们归功于 argparse。
setup.py 的相关部分:
setup(
# (...)
zip_safe=True,
scripts=['bin/bgce.py', 'bin/sizes.py'],
packages=find_packages(),
data_files=data_files,
entry_points = {
'console_scripts': [
'bgce = bgce:main',
'sizes = sizes:main',]
}
)
我最终在 /usr/local/bin 中得到 bgce、bgce.py、sizes、sizes.py。 4 个都工作。
如果我遗漏了包或脚本行,则没有重复项,但文件会像这样失败:
Traceback (most recent call last):
File "/usr/local/bin/bgce", line 9, in <module>
load_entry_point('Backtestground==1.0', 'console_scripts', 'bgce')()
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 305, in load_entry_point return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 2244, in load_entry_point return ep.load()
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 1954, in load
entry = __import__(self.module_name, globals(),globals(), ['__name__'])
ImportError: No module named bgce
我该怎么做才能只安装 bgce 和尺寸,没有附加烦人的(用于制表符完成).py 的重复项?
I have 2 Python scripts that can be used from the shell as they are thanks to argparse.
The relevant part of setup.py:
setup(
# (...)
zip_safe=True,
scripts=['bin/bgce.py', 'bin/sizes.py'],
packages=find_packages(),
data_files=data_files,
entry_points = {
'console_scripts': [
'bgce = bgce:main',
'sizes = sizes:main',]
}
)
I end up with bgce, bgce.py, sizes, sizes.py in /usr/local/bin. All 4 work.
If I leave out either the packages or the scripts line, there are no duplicates, but the files fail like this:
Traceback (most recent call last):
File "/usr/local/bin/bgce", line 9, in <module>
load_entry_point('Backtestground==1.0', 'console_scripts', 'bgce')()
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 305, in load_entry_point return get_distribution(dist).load_entry_point(group, name)
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 2244, in load_entry_point return ep.load()
File "/usr/lib/python2.6/dist-packages/pkg_resources.py", line 1954, in load
entry = __import__(self.module_name, globals(),globals(), ['__name__'])
ImportError: No module named bgce
What can I do to only have bgce and sizes installed, no duplicates with annoying (for tab-completion) .py attached?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保实际模块具有如下所示的 main 函数:
我们经常采用在
if __name__ == "__main__" 下方编写主代码的模式:
如果没有实际的 main(),distutils 就无法获得加载点。 (捕获 ^C 是可选的:)我也遇到了这个问题,直到我意外地发现我的一个模块实际上可以工作,而其他模块却不能。这就是差别,改正之后就好了!
Make sure that the actual modules have a main function like this:
We often do the pattern of writing out main code below the
if __name__ == "__main__" :
and if there isn't an actual main(), distutils can't get a load point. (Catching ^C is optional :) I had this problem too until I accidentally discovered that one of my modules actually worked while the others didn't. This was the difference, once corrected, it's all good!
IIRC:将bgce.py 和sizes.py 作为包的一部分,删除脚本参数,保留入口点。
IIRC: Have bgce.py and sizes.py part of your packages, remove the scripts argument, keep the entry points.