Search code examples
pythoninstallationdistutils

install Python module both as module and as script


Say I wrote module foo.py.

I want the installation process to copy foo.py to prefix/lib/pythonX.Y/site-packages so that it can be imported by other modules, but also to create a symbolic link named foo (not foo.py) in prefix/bin/ that points to foo.py.

How does one tell distutils to do that?


Solution

  • You can do this if you use setuptools entry_points. Here's an example:

    foo.py

    def main():
        print "Hello world"
    

    setup.py

    from setuptools import setup
    
    setup(
        name="foo",
        version = "0.1",
        py_modules=['foo'],
        entry_points = {
            'console_scripts': ['foo = foo:main']
        }
    )
    

    Example usage, once the module has been installed using a tool like pip:

    $ foo
    Hello world
    $ python -c 'import foo; foo.main()'
    Hello world