How to publish a Python package to PyPI

PyPI is the Python Package Index, that archive that let you install a package using pip, for example: pip install Flask

In the past days I started writing a Python API client for Toshl expense manager and I decided to publish the library on PyPI. You can have a look at my library here https://github.com/andreagrandi/toshl-python (please note: it’s still in development and Toshl API is not even public yet) in case you are not sure how to structure it.

I found a nice guide but it wasn’t complete (for example it didn’t say how to sign packages) so I decided to rewrite it adding more information.

Create PyPI accounts

To publish packages on PyPI you need to create two accounts: one for the production server and another one for the test server. When you register, please specify (if you have one, but I really hope you do) the PGP id of your public key. Once the accounts are created, you need to create a file named .pypirc in your $HOME folder containing the following configuration:

Please substitute your_username and your_password with the details you sent during the registration.

Preparing the package

I assume you have structured your library in the proper way and have included a setup.py with all the configuration (it’s not something specific to PyPI so you should have done it already). If you haven’t I remember you can give a look at my library here https://github.com/andreagrandi/toshl-python in particular to the setup.py:

Upload the package to PyPI Test server

The first time you upload the package you will need to register it:

and then you will need to build the package and upload it (please note I’m using the –sign to sign the package with PGP):

Upload the package to PyPI production server

Once you have verified that you are able to build and upload the package to the test server (without getting any errors), you should upload it to the production server:

This is everything you need to do if you want to publish a Python package on PyPI. Happy coding!