How to create a pip package?

Last updated: Sun, 02 Jun 2019 18:59:03 GMT 0 comments

In this first post you will find quick references to the commands needed to share your package through Pypi.

Pypi, available at https://pypi.python.org, is the official repository for Python packages. Packages available on Pypi can be installed very easily using pip install <package_name>.

Registering your package and first upload

To make your Python package available on Pypi, you will need to:

  1. Create configuration files (including setup.py, setup.cfg) for your project, cf. https://packaging.python.org/distributing/#configuring-your-project for a full list.
  2. Build your package
cd <PATH_TO_PACKAGE>
python setup.py sdist
python setup.py bdist_wheel
  1. Register your project and upload to Pypi test server
python setup.py register -r https://testpypi.python.org/pypi
twine upload -r pypitest dist/*
  1. Check that the install works fine from Pypi test server
pip install -i https://testpypi.python.org/pypi <PACKAGE_NAME>
  1. Upload your package to Pypi!
twine upload -r pypi dist/*

Releasing a new version

To make a new version of your package available:

  1. Delete any previous built
cd <PATH_TO_PACKAGE>
rm -rf dist
rm -rf build
rm -rf *.egg-info
  1. Rebuild your package
python setup.py sdist
python setup.py bdist_wheel
  1. Send to Pypi test server and check the installation
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
pip install --index-url https://test.pypi.org/simple/ <PACKAGE_NAME>

Check that the version is the latest one:

pip freeze | grep <PACKAGE_NAME>
  1. Upload to Pypi!
twine upload -r pypi dist/*

For more information, the official documentation (with examples) is available at: https://packaging.python.org/distributing

Edit on July 6, 2018: replacing '/pypi' by '/simple' when installing from pypi test. Edit on June 2, 2019: updated twine and pip install commands to upload and install from pypi test according to TestPypi documentation. Add info about checking version.