Search code examples
pythonterminalcommand-line-interfacesetuptools

How to make globally callable python program?


I wrote a module and created a setup.py to install the module:

from setuptools import setup, find_packages

setup(
    name='mymodule',
    version='0.1',
    packages=find_packages(exclude=['test', 'test.*']),
    include_package_data=True,
    platforms='any',
    install_requires=[
        'lxml==3.3.5',
        'Pillow==3.0.0',
        'requests==2.2.1',
        'xmltodict==0.10.1',
        'pdfrw==0.2',
        'python-dotenv==0.4.0',
        'boto==2.39.0'
    ],
)

In the same module I also wrote a command line interface for the module using getopt. I want to make this command line interface globally available so that any user on the system can do things like:

$ mycliprogram -i inputfile.xml -o outputfile.txt

Does anybody know how I can include mycliprogram.py in setup.py so that anybody on the system can use it from the command line?


Solution

  • You can either use console-scripts (as Sergey suggested) or the entry_points parameter in your setup():

      entry_points={
          'console_scripts': [
              'mycliprogram=mymodule:whatever',
          ],
      },
    

    This will create a myclyprogram wrapper, which is accessible via $PATH and it will call whatever in mymodule. So if you install your module via pip or setup.py, you'll be able to call mycliprogram with whatever options you defined directly from the command line prompt.

    More information: Command Line Scripts – Python Packaging Tutorial