Search code examples
pythoncommand-line-interfacesetuptoolspython-click

Making a neat, installable Python library with Click


I'm trying to make a command line tool with Click in Python, and I can't seem to find any documentation on packaging up the library into something that's installable. Is there any way to do this? At the moment I'm just using a virtual environment and installing it for testing using the commands listed in the docs: (http://click.pocoo.org/4/setuptools/#testing-the-script)

$ virtualenv venv
$ . venv/bin/activate
$ pip install --editable .

I'm relatively new to Click so forgive me if I'm missing something painfully obvious.


Solution

  • If you've followed the Setuptools Integration steps in the article you linked to, you're most of the way there. Try installing the package as if it came from pip (maybe in a different virtualenv):

    $ virtualenv deploy
    $ source deploy/bin/activate
    $ pip install .
    

    Then you can invoke your command as normal - it'll be installed under the bin directory in the virtualenv. It's a good idea to try testing the command from somewhere else to make sure you don't have a dependency on being inside the project directory (like you've probably been doing during testing).

    Once you're happy that it installs correctly and all the imports work as expected, you can proceed to register your package with PyPI (the Package Index). You can read about this in the Python Docs

    That's about it really - setuptools/Click does most of the heavy lifting.