Search code examples
pythonpypipyproject.toml

An updated guide on releasing and versioning PyPi packages


I have recently bumped into an issue while trying to release a new version of my PyPi package.

After updating the source code (and preparing the files for the release), I cannot find any updated guide on how to release a new version (see this, for instance) to PyPi.

Most guides reference setup.py, which has now been replaced by pyproject.toml.

So, from Windows (IDE: VScode), the old command

py setup.py sdist bdist_wheel

does not work anymore. When replacing setup.py with pyproject.toml I get the following error:

File "C:\Users\generic\pyproject.toml", line 3
    build-backend = "hatchling.build"
   ^
SyntaxError: cannot assign to operator

My pyproject.toml is built like any generic pyproject.toml file

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "example_package_YOUR_USERNAME_HERE"
version = "0.0.2"
authors = [
  { name="Example Author", email="[email protected]" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]

[project.urls]
"Homepage" = "https://github.com/pypa/sampleproject"
"Bug Tracker" = "https://github.com/pypa/sampleproject/issues"

I am aware of the Python Packages guide. However, they explain how to release using git, and I'd like an equivalent for the old method.

As user @phd pointed out, there are some guides out there. However, these only mention how to upload a new package to PyPi, not a new distribution release. Also, they do not address uploads with API tokens. Am I supposed to create a new API token? Old tokens are not accessible anymore but new ones return the following error:

ERROR    HTTPError: 403 Forbidden from https://test.pypi.org/legacy/
         Invalid or non-existent authentication information. See https://test.pypi.org/help/#invalid-auth for more information.

Solution

  • Solved:

    For reference, see Trouble following packaging libraries tutorial: upload #313. I was also confused about whether to use twine upload --repository testpypi dist* to update a package or twine upload --repository-url URL dist/* to update an existing package. The documentation on the issue is not clear to me. Also, I have encountered all sorts of issues while using API tokens for authentication. What worked for me was the following suggestion:

    The correct URLs for the repositories are https://upload.pypi.org/legacy/ and https://test.pypi.org/legacy/.

    So, if you wish to update an existing package using an API token:

    • change the version of the project in pyproject.toml from, e.g., 0.0.1 to 0.0.2
    • -m build
    • -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
    • generate a project-scoped API token on PyPi and use it for authentication