Home Upload a package to PyPI
Post
Cancel

Upload a package to PyPI

Although the Packaging Python Projects tutorial is very detailed in how to package a Python Project, I encountered some difficulties at the time of do it, so I’m going to leave here a summary and some notes to my future me ;)

First of all, if we want made our application available as a command-line tool, we need to define an entry point in our pyproject.toml:

1
2
[project.scripts]
projectname = "projectname.projectname:main"

Creating accounts

We will need two accounts:

Updating the tools

We will need to have installed the last version of build and twine:

1
python3 -m pip install --upgrade build
1
python3 -m pip install --upgrade twine

Building

Run this command from the same directory where pyproject.toml is located:

1
python3 -m build

Configuring the access to test.pypi.org

We will need to create a PyPI API token to securely upload our project. We can create one at https://test.pypi.org/manage/account/#api-tokens

To avoid having to copy and paste the token every time you upload, we can create a $HOME/.pypirc file:

1
2
3
4
5
6
7
8
9
10
11
12
13
[distutils]
  index-servers =
    testpypi
    projectname

[testpypi]
  username = __token__
  password = pypi-token

[fail2bangeolocation]
  repository = https://test.pypi.org/legacy/
  username = __token__
  password = pypi-token

Uploading the package to testpypi

1
python3 -m twine upload --repository testpypi dist/*

Checking test package installation

1
python3 -m pip install --index-url https://test.pypi.org/simple/ projectname

If everything went well…

Uploading the package to PyPI

1
python3 -m twine upload dist/*

Checking package installation

1
pip3 install projectname

Sources:

Enjoy! ;)

This post is licensed under CC BY 4.0 by the author.