Dropping support for older Python versions¶
The ability to drop support for older Python versions is enabled by the standard Core metadata specifications 1.2 specification via the “Requires-Python” attribute.
Metadata 1.2+ installers, such as Pip, will adhere to this specification by matching the current Python runtime and comparing it with the required version in the package metadata. If they do not match, it will attempt to install the last package distribution that supported that Python runtime.
This mechanism can be used to drop support for older Python versions, by amending the Requires-Python
attribute in the package metadata.
Requirements¶
This workflow requires that the user installing the package uses Pip [1], or another installer that supports the Metadata 1.2 specification.
Dealing with the universal wheels¶
Traditionally, Setuptools projects providing Python code that is semantically
compatible with both Python 2 and Python 3, produce wheels that have a py2.py3
tag in their names. When dropping
support for Python 2, it is important not to forget to change this tag
to just py3
. It is often configured within setup.cfg
under
the [bdist_wheel]
section by setting universal = 1
.
If you use this method, either remove this option or section, or
explicitly set universal
to 0
:
# setup.cfg
[bdist_wheel]
universal = 0 # Make the generated wheels have "py3" tag
Hint
Regarding deprecated direct setup.py
invocations,
passing the --universal
flag on the command line could override this setting.
Defining the Python version required¶
1. Install twine¶
Ensure that you have twine available at its latest version. Steps:
python3 -m pip install --upgrade twine
py -m pip install --upgrade twine
2. Specify the version ranges for supported Python distributions¶
Set the version ranges declaring which Python distributions are supported
within your project’s pyproject.toml
. The requires-python configuration field
corresponds to the Requires-Python core metadata field:
[build-system]
...
[project]
requires-python = ">= 3.8" # At least Python 3.8
You can specify version ranges and exclusion rules (complying with the Version specifiers specification), such as at least Python 3.9. Or, at least Python 3.7 and beyond, skipping the 3.7.0 and 3.7.1 point releases:
requires-python = ">= 3.9"
requires-python = ">= 3.7, != 3.7.0, != 3.7.1"
If using the Setuptools build backend, consult the dependency-management documentation for more options.
Caution
Avoid adding upper bounds to the version ranges, e. g. ">= 3.8, < 3.10"
. Doing so can cause different errors
and version conflicts. See the discourse-discussion for more information.
3. Validating the Metadata before publishing¶
Within a Python source package (the zip or the tar-gz file you download) is a text file called PKG-INFO.
This file is generated by the build backend when it generates the source package. The file contains a set of keys and values, the list of keys is part of the PyPA standard metadata format.
You can see the contents of the generated file like this:
tar xfO dist/my-package-1.0.0.tar.gz my-package-1.0.0/PKG-INFO
Validate that the following is in place, before publishing the package:
If you have upgraded correctly, the
Metadata-Version
value should be 1.2 or higher.The
Requires-Python
field is set and matches your specification in the configuration file.
4. Publishing the package¶
Proceed as suggested in Uploading your Project to PyPI.
Dropping a Python version¶
In principle, at least metadata support for Python versions should be kept as long as possible, because
once that has been dropped, people still depending on a version will be forced to downgrade.
If however supporting a specific version becomes a blocker for a new feature or other issues occur, the metadata
Requires-Python
should be amended. Of course this also depends on whether the project needs to be stable and
well-covered for a wider range of users.
Each version compatibility change should have its own release.