pythonpipsetuptools

How to add command line tool to python package


I have written a simple package called fcards that is currently on PyPI. The package provides a curses based interface for creating and practicing flash cards, and right now, in order to run it, the user must type "python -m fcards.fcards". I would like to know how to make it so the user can simply type "fcards" to run the application.

I've tried using the "scripts" and "entry_points" arguments in the setup function in my setup.py file but so far nothing has worked consistently on all platforms.


Solution

  • Usually, I write a console driver and put it in a file console.py as the main function.

    Then in setup.py:

    entry_points={
        'console_scripts': ['<name> = <name>.console:main'],
    },
    

    where <name> is the name of the module. This has worked out-of-the-box on UNIX-like platforms. It also worked using the Anaconda Python distribution on ms-windows.

    On ms-windows the distinction between console_scripts and gui_scripts is important, because the latter have to be associated with another python binary. This is done by giving them the extension pyw instead of py. UNIX-like platforms (incuding macOS, I guess) generally don't care.