How to Upload Your Packages to PyPI by an API Token

This post tells you how to upload your package to PyPI by an API token.

Create your PyPI account

Skip this step if you’ve already got an account.

You can go to PyPI website to register an account.

Create an API token

Skip this step if you’ve already got a token for your device and your project.

Please sign in PyPI website and click your name then click Account settings.

Then click Add API token in the API tokens section, set the token name and scope, and click Add token.

Please copy the token immediately since the API token will only be displayed once. If you forget to do that, revoke and create a new one.

Edit your .pypirc file in your project directory

If you don’t have a .pypirc file in your project directory, please create it.

Then write the following content into the .pypirc file.

1
2
3
[pypi]
username = __token__
password = pypi-AgEIcH...

Note that username is always __token__ and password is your API token which has been created in the previous step.

Install twine tool

Skip this step if you’ve already installed it in your Python environment.

1
pip install --upgrade twine

Check your package files

Run the following script to ensure your package files are valid.

1
twine check dist/*

Upload your packages to PyPI

Run the following script to upload your packages to PyPI.

1
twine upload dist/*

You should not be prompted for a username or password, since those are configure in your .pypirc.

Buy a cup of coffee for the author.