How to publish a Python package to PyPI

Posted on Sun 10 April 2016 in Development • Tagged with pip, pypi, Python

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 (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:

index-servers =



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 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 in particular to the

from setuptools import setup, find_packages

    author='Andrea Grandi',
    author_email='[email protected]',
    description='Python client library for Toshl API.',

Upload the package to PyPI Test server

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

python register -r pypitest

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):

python sdist upload --sign -r pypitest

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:

python register -r pypi
python sdist upload --sign -r pypi

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