Uploading to PyPI

suggest change

Once your setup.py is fully functional (see Introduction), it is very easy to upload your package to PyPI.

Setup a .pypirc File

This file stores logins and passwords to authenticate your accounts. It is typically stored in your home directory.

# .pypirc file

[distutils]
index-servers =
  pypi
  testpypi

[pypi]
repository = https://upload.pypi.org/legacy/
username = __token__
password = your_pypi_token

[testpypi]
repository = https://test.pypi.org/legacy/
username = __token__
password = your_testpypi_token

https://pypi.python.org/pypi and https://testpypi.python.org/pypi are legacy upload hosts. Current twine rejects them. PyPI no longer accepts an account password for uploads; the password field is an API token and the username is __token__. It is safer to use twine for uploading packages, so make sure that is installed.

$ pip install twine

Register and Upload to testpypi (optional)

Note: PyPI does not allow re-uploading a file name that was already published, so it is prudent to first test your deployment on a dedicated test server, e.g. TestPyPI. Consider a versioning scheme for your package prior to uploading such as calendar versioning or semantic versioning.

Create an account at TestPyPI the first time you upload there. python setup.py register is not supported on PyPI or TestPyPI. From the directory that contains setup.py or pyproject.toml, build the archives and upload them:

$ python -m pip install build twine
$ python -m build
$ twine upload dist/* -r testpypi

Your package should now be accessible through your account.

Testing

Make a test virtual environment. Try to pip install your package from either testpypi or PyPI.

# Using virtualenv
$ mkdir testenv
$ cd testenv
$ virtualenv .virtualenv
...
$ source .virtualenv/bin/activate
# Test from testpypi
(.virtualenv) $ python -m pip install --verbose --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ package_name
...
# Or test from PyPI
(.virtualenv) $ pip install package_name
...

(.virtualenv) $ python
Python 3.5.1 (default, Jan 27 2016, 19:16:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import package_name
>>> package_name.foo()
100

If successful, your package is at least importable. You might consider testing your API as well before your final upload to PyPI. If your package failed during testing, do not worry. You can still fix it, re-upload to TestPyPI and test again. The interpreter banner above is from a 2016 session and was not re-run.

Register and Upload to PyPI

Make sure twine is installed:

$ pip install twine

Either log in, or create a new account at PyPI.

$ python -m build
$ twine upload dist/*

That’s it! Your package is now live.

If you discover a bug, simply upload a new version of your package.

Documentation

Don’t forget to include at least some kind of documentation for your package. PyPI takes as the default formatting language reStructuredText.

Readme

If your package doesn’t have a big documentation, include what can help other users in a README.rst file. description-file in setup.cfg is not a setuptools field that publishes that file. Pass the text to setup():

from pathlib import Path
from setuptools import setup

long_description = Path("README.rst").read_text(encoding="utf-8")
setup(
    long_description=long_description,
    long_description_content_type="text/x-rst",
)

A Markdown README is rendered when long_description_content_type is "text/markdown". Without that content type, PyPI does not treat the file as formatted Markdown.

Licensing

It’s often more than welcome to put a LICENSE.txt file in your package with one of the OpenSource licenses to tell users if they can use your package for example in commercial projects or if your code is usable with their license.

In more readable way some licenses are explained at TL;DR.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents