放弃对旧版 Python 的支持¶
Dropping support for older Python versions is supported by the standard Core metadata specifications 1.2 specification via a “Requires-Python” attribute.
Metadata 1.2+ clients, such as Pip 9.0+, 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.
This guide is specifically for users of setuptools, other packaging tools such as flit
may offer similar functionality but users will need to consult relevant documentation.
要求¶
这个工作流程要求:
The publisher is using the latest version of setuptools,
twine 的最新版本被用来上传软件包,
The user installing the package has at least Pip 9.0, or a client that supports the Metadata 1.2 specification.
Dealing with the universal wheels¶
Traditionally, 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 they
use setuptools.
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
小技巧
Since it is possible to override the setup.cfg
settings via
CLI flags, make sure that your scripts don’t have --universal
in
your package creation scripts.
定义所需的 Python 版本¶
1.下载最新版本的 Setuptools¶
确保在生成源码发行版或二进制发行版之前,更新 Setuptools 并安装 twine。
步骤:
python3 -m pip install --upgrade setuptools twine
py -m pip install --upgrade setuptools twine
setuptools 版本应该在 24.0.0 以上。
2. Specify the version ranges for supported Python distributions¶
You can specify version ranges and exclusion rules, such as at least Python 3. Or, Python 2.7, 3.4 and beyond.
例子:
Requires-Python: ">=3"
Requires-Python: ">2.7,!=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*"
The way to set those values is within the call to setup
within your
setup.py
script. This will insert the Requires-Python
metadata values based on the argument you provide in python_requires
.
from setuptools import setup
setup(
# Your setup arguments
python_requires='>=2.7', # Your supported Python ranges
)
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 Distutils or setuptools 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 setup.py.
4. Using Twine to publish¶
Twine has a number of advantages, apart from being faster it is now the supported method for publishing packages.
Make sure you are using the newest version of Twine, at least 1.9.
Dropping a Python release¶
Once you have published a package with the Requires-Python metadata, you can then make a further update removing that Python runtime from support.
It must be done in this order for the automated fallback to work.
For example, you published the Requires-Python: “>=2.7” as version 1.0.0 of your package.
If you were then to update the version string to “>=3.5”, and publish a new version 2.0.0 of your package, any users running Pip 9.0+ from version 2.7 will have version 1.0.0 of the package installed, and any >=3.5 users will receive version 2.0.0.