How to release a new Python package version to PyPI?

python

These are the steps I take when releasing a version of my Python package to PyPI.

First run your checks/cleaning. I have a post about this here. If any fail, start again.

Run tests.

pytest
rm -r .tox; tox  # Continue below while this is running

In a new branch (such as release/x.x.x):

Once the release is uploaded, there’s no way to change the README.md on PyPI, or the documentation on Readthedocs, without releasing another version. So take care with these in stable releases.

Commit, push and merge into main.

git commit -a
git push origin head

Pull main, tag the commit and push the tag.

git checkout main
git pull
git tag x.x.x
git push --tags

Create the package.

pip install --upgrade pip twine build
python -m build --wheel

Upload the package.

twine check dist/mypackage-x.x.x.whl
twine upload dist/mypackage-x.x.x.tar.gz

Update coverage badge.

pip install --upgrade pytest-cov coveralls pyyaml
pytest --cov-branch --cov-report term-missing --cov mypackage tests
coveralls

Build readthedocs if there were any documentation changes.

Update related blog posts/Stack Overflow posts.