A recipe on how to publish a simple package (i.e., pure Python) to PyPi.
First, setup your ~/.pypi
to make things a little easier:
[distutils]
index-servers =
pypi
testpypi
[pypi]
repository = https://upload.pypi.org/legacy/
[testpypi]
repository = https://test.pypi.org/legacy/
This way, it is possible to use twine
to upload by using just testpypi
instead of https://test.pypi.org/legacy/
.
Go to the source directory where your setup.py
is and run:
cd MyPackage
python setup.py bdist_wheel sdist
This will create a .tar.gz
with the source code and a .whl
, which is a binary package. Check the packages created:
twine check dist/*
If something fails, you can fix it and check it again until everything PASS
.
Upload first you package to https://test.pypi.org and test the installation. This is a very important step because it is when you will fix problems related to package dependencies:
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
And test the installation:
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ MyPackage
Notes:
-
Test the installation of you package in virtual environments:
conda create -n MyPackage python=3.x.y conda activate MyPackage pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ MyPackage
or:
python -m venv MyPackage source MyPackage/bin/activate pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ MyPackage
-
The options
--index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/
are important since they will allowpip
to download dependencies packages from both https://test.pypi.org/ (whereMyPackage
is) and https://pypi.org (where probably the dependencies are).
Once everythings works as it should, publish it to the official PyPi.
Use twine
to publish MyPackage to https://pypi.org:
twine upload dist/*
And now MyPackage
can be easely installed with:
pip install MyPackage