Skip to content

Tutorial for phasing out Python versions #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions source/guides/dropping-older-python-versions.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
.. _`Dropping Support for Older Python Versions`:

==========================================
Dropping Support for Older Python Versions
==========================================

Dropping support for older Python versions is supported by the standard :ref:`core-metadata` 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should include a caveat here that this guide is specifically for users of setuptools, or users of distutils that will need to upgrade to setuptools in order to be able to include the Requires-Python field in their metadata.

Users of other packaging tools will need to consult the relevant project's documentation on how to set Requires-Python, but the general approach described here will otherwise hold.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in top comment

This guide is specifically for users of :ref:`setuptools`, other packaging tools such as ``flit`` may offer similar functionality but users will need to consult relevant documentation.

Requirements
------------

This workflow requires that:

1. The publisher is using the latest version of :ref:`setuptools`,
2. The latest version of :ref:`twine` is used to upload the package,
3. The user installing the package has at least Pip 9.0, or a client that supports the Metadata 1.2 specification.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's potentially a 4th requirement here, which is that the package index server has to populate the data-requires-python link attribute described in PEP 503. Both pypi.org (the warehouse codebase) and pypi.python.org (the legacy PyPI codebase) do that, but we're not sure about the various caching proxies that are out there - those may confound the client's version requirement detection.

However, I think we can leave that out for now, and potentially add it if folks report problems that get traced back to that issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave this out I think


Defining the Python version required
------------------------------------

1. Download the newest version of Setuptools
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requirements also say the latest version of Pip and Twine are needed.

Shall we make this section cover all of those, and update the command to pip install --upgrade pip setuptools twine all at once?

Or, because Pip can be a bit of a special case, at least for Setuptools and Twine.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ensure that before you generate source distributions or binary distributions, you update Setuptools and twine.

Steps::

pip install --upgrade setuptools twine

Version should be above 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.

Examples::

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`.

..code-block:: Python

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I think this section will be fine if we add an "Aimed at distutils/setuptools users" caveat at the top, it does suggest to me that a twine upload --dry-run option that printed out the headers that would be sent without actually making a POST request to the server could be very helpful. (I'll add a comment to pypa/twine#207 pointing back to this PR as an example use case)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


This file is generated by Distutils or :ref:`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 xvfz dist/my-package-1.0.0.tar.gz -O | cat */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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to mentioning support here, I'd also suggest noting that it's the most reliable when it comes to correctly passing the artifact metadata to the publication server (the server doesn't inspect the artifacts directly - it relies on the client to supply the relevant metadata).


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 fail-back 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.
1 change: 1 addition & 0 deletions source/guides/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ introduction to packaging, see :doc:`/tutorials/index`.
multi-version-installs
single-sourcing-package-version
supporting-multiple-python-versions
dropping-older-python-versions
packaging-binary-extensions
supporting-windows-using-appveyor
packaging-namespace-packages
Expand Down