Testing on TestPyPI
Once your setup.py
works (i.e. that your library shows up in the pip list
and that you can import your library in a python script/consol using import my_library
) you can start to think about how to distribute it.
Uploading your code to PyPI is the most common way to do so.
First, you need to check that the name of your library does not already exists by browsing the PypI website and search for the library name you want to distribute. If there is no result for your library name you are good to go, otherwise you will need to change your library name.
Then you need to test that you can properly distribute it on a server. To avoid polluting the PyPI index (that will affect the entire Python community) another server exists for testing this step: TestPyPI
You need to ensure that you have the latest version of setuptools
and wheel
libraries:
python3 -m pip install --upgrade setuptools wheel twine
Before distributing your project to a server you need to build it locally:
python3 setup.py sdist bdist_wheel
This will generate distribution archives in the dist
directory.
To upload you newly-builded library to testPypi you need to use twine
:
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
And you can now install it from this distant server:
python3 -m pip install --index-url https://test.pypi.org/simple/ my_project
Of course, you should install it on a Python version that does not have the library installed yet!
Now let’s say an obvious thing: You need to test that you can properly upload to the TestPyPI server and install your library from it before moving to PyPI…
If you want more info on this you can read the setuptools’ packaging project tutorials.
Upload to PyPI
To upload your package to PyPI (the REAL Python Index) you need to use the following command:
twine upload dist/*
This will ask you your PyPI username and password.
Once the upload is done you can pip install you package from anywhere (if you have an internet connection of course!):
pip install my_project
Once you have made a change in your project you might want to re-upload your library to PyPI. To do so you need to:
- update the version number according to the versioning strategy defined here
- re-run the
python3 setup.py sdist bdist_wheel
command, - re-run the
twine upload dist/my_new_version*
command.
Et voila!