如何使用 setuptools 安装 python cli 脚本而不重复?

发布于 2024-10-06 07:01:08 字数 1161 浏览 5 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残疾 2024-10-13 07:01:08

确保实际模块具有如下所示的 main 函数:

def main():
    try:
        some_stuff()
    except KeyboardInterrupt :
        print ""
        sys.exit()

if __name__ == "__main__" :
    main()

我们经常采用在 if __name__ == "__main__" 下方编写主代码的模式:
如果没有实际的 main(),distutils 就无法获得加载点。 (捕获 ^C 是可选的:)我也遇到了这个问题,直到我意外地发现我的一个模块实际上可以工作,而其他模块却不能。这就是差别,改正之后就好了!

Make sure that the actual modules have a main function like this:

def main():
    try:
        some_stuff()
    except KeyboardInterrupt :
        print ""
        sys.exit()

if __name__ == "__main__" :
    main()

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!

眼前雾蒙蒙 2024-10-13 07:01:08

IIRC:将bgce.py 和sizes.py 作为包的一部分,删除脚本参数,保留入口点。

IIRC: Have bgce.py and sizes.py part of your packages, remove the scripts argument, keep the entry points.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文