Getting setup.py to add commands to the path


There’s a number of articles out there that mention this, but they’re a bit of a pain to follow. Of them all, I found this set of docs to be the most useful, but still lacking.

This may, or may not, have been the case if I had read the entire docs, but that’s ok. This blog post is mainly designed just as a reference, for people who want to get their answers quickly, as I did, and without trial and error!

For those that want a quick answer, when you’re using setuptools in your setup function, you want to make use the of the entry_points keyword.

From the example above:

setup(
    ...
    entry_points = {
        'console_scripts': ['funniest-joke=funniest.command_line:main'],
    }
    ...
)

As a quick explanation of how this works:

  • funniest-joke – This is the command that you’ll enter into your terminal window
  • funniest.command_line:main – This tells setuptools which file, and which function to call. In this example, it will go into the funniest module, load the command_line file, and execute the main function