开发者

How to install python cli scripts with setuptools without duplicates?

开发者 https://www.devze.com 2023-01-28 05:52 出处:网络
I have 2 Python scripts that can be used from the shell as they are thanks to argparse. The relevant part of setup.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开发者_如何学C only have bgce and sizes installed, no duplicates with annoying (for tab-completion) .py attached?


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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号