A guide on how to publish a simple package (i.e., pure Python) to PyPi.
First, set up your ~/.pypirc
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, you can use twine
to upload by simply specifying 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
file with the source code and a .whl
file, which is a binary package. Check the created packages:
twine check dist/*
If something fails, you can fix it and check again until everything PASS
es.
First, upload your package to https://test.pypi.org and test the installation. This is a very important step because it allows you to fix problems related to package dependencies:
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
Then, 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 your 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 because they allowpip
to download dependency packages from both https://test.pypi.org/ (whereMyPackage
is) and https://pypi.org (where the dependencies are likely located).
Once everything works as it should, publish it to the official PyPi.
Use twine
to publish MyPackage
to https://pypi.org:
twine upload dist/*
Now, MyPackage
can be easily installed with:
pip install MyPackage