Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install python cli scripts with setuptools without duplicates?

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?

like image 362
thorwil Avatar asked Oct 13 '22 20:10

thorwil


1 Answers

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!

like image 53
l0r3zz Avatar answered Oct 18 '22 01:10

l0r3zz