Skip to content

Commit 552daaa

Browse files
authored
Merge pull request #790 from InessaPawson/req-python
docs: clarify recommended use of requires-python
2 parents 9fbe2e3 + bfaf325 commit 552daaa

1 file changed

Lines changed: 25 additions & 48 deletions

File tree

tutorials/pyproject-toml.md

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ Following that you learned how to add a:
1717

1818
to the root of your project directory.
1919

20-
To enhance the visibility of your package on PyPI and provide more information
21-
about its compatibility with Python versions, project development status, and
22-
project maintainers, you should add additional metadata to your `pyproject.toml`
23-
file. This
24-
lesson will guide you through the process.
20+
To enhance the visibility of your package on PyPI and provide information
21+
about its development status and maintainers, you should add additional
22+
metadata to your `pyproject.toml` file. This lesson will guide you through
23+
the process.
2524

2625

2726
:::{admonition} Learning Objectives
@@ -88,14 +87,14 @@ build-backend = "hatchling.build"
8887

8988
The pyproject.toml file tells your build tool:
9089

91-
- What {term}`Build backend` to use to build your package (we are using
90+
- what {term}`Build backend` to use to build your package (we are using
9291
{term}`Hatchling` in this tutorial but there are
9392
[many others to choose from](/package-structure-code/python-package-build-tools)).
94-
- How and where to retrieve your package's version:
93+
- how and where to retrieve your package's version:
9594
- **statically** where you declare the version `version = "0.1.0"` or
9695
- **dynamically** where the tool looks to the most recent tag in your history to determine the current version.
97-
- What {term}`Dependencies` your package needs
98-
- What versions of Python your package supports (important for your users).
96+
- what {term}`Dependencies` your package needs,
97+
- it can also declare Python-version requirements if your package has them.
9998

10099
The `pyproject.toml` file also makes it easy for anyone browsing your GitHub
101100
repository to quickly understand your package's structure such as:
@@ -315,52 +314,35 @@ If you have multiple licenses, or a custom license, you can also express these u
315314

316315
If you want to distribute license files, or other files containing legal information, with your package, you can include these using the [`license-files`](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license-files) entry, but this is not required.
317316

318-
### Step 3: Specify Python version with `requires-python`
319-
320-
Add the `requires-python` field to your `pyproject.toml` `[project]` table.
321-
The `requires-python` field helps pip identify which Python versions that your package supports.
322-
It is set to a single value.
323-
The [packaging specification](https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata-requires-python) defines`requires-python` as a string that uses version specifiers. Most projects will specify the oldest Python version supported by the package. In some advanced cases, an upper bound is set to indicate which future Python versions, if any, will be supported.
324-
325317
:::{admonition} But how do I figure out which Python versions I should support?
326318
:class: tip
327319
Good question. The Python developer guide provides a [status page](https://devguide.python.org/versions/) (and a handy visualization) that explains the status of each Python release. Python releases go through several different phases that are explained in [PEP 602](https://peps.python.org/pep-0602/).
328320

329321
We recommend that you use the latest Python release in the **bugfix** phase. If your Python release is in the **security** phase, we recommend migrating to a newer version of Python.
330-
331-
[SPEC 0](https://scientific-python.org/specs/spec-0000/) of the Scientific Python project suggests a common schedule for dependencies, including Python release versions, and is also worth considering for your project.
332322
:::
333323

334-
{emphasize-lines="22"}
335-
```toml
336-
[build-system]
337-
requires = ["hatchling"]
338-
build-backend = "hatchling.build"
324+
:::{admonition} When should I use `requires-python`?
325+
:class: tip
326+
You do not need to specify `requires-python` for every package. However, if you
327+
know that your package will not work with older versions of Python, use `requires-python`
328+
to prevent installers from installing it with those versions. For example, if your package
329+
requires features introduced in Python 3.10:
339330

331+
```toml
340332
[project]
341-
name = "pyospackage"
342-
version = "0.1.0"
343-
description = """
344-
Tools that update the pyOpenSci contributor and review metadata
345-
that is posted on our website
346-
"""
347-
authors = [
348-
{ name = "Firstname Lastname", email = "email@pyopensci.org"},
349-
{ name = "Secondperson Fullname", email = "email2@pyopensci.org" }
350-
]
351-
maintainers = [
352-
{ name = "Secondperson Fullname", email = "email2@pyopensci.org" },
353-
{ name = "New Friend", email = "newbie@pyopensci.org" }
354-
]
355-
readme = "README.md"
356-
license = "MIT"
357333
requires-python = ">=3.10"
358334
```
359335

360-
### Step 4: Specify Dependencies
336+
This helps `pip` select a compatible release of your package for the Python version a user is running.
337+
:::
338+
339+
### Step 3: Specify dependencies
361340

362341
Next add your dependencies table to the project table.
363-
The `dependencies =` section contains a list (or array in the toml language) of the Python packages that your package requires to run properly in a Python environment. Similar to the requirements listed in the `[build-system]` table above:
342+
The `dependencies =` section contains a list (or array in the toml language) of
343+
the Python packages that your package requires to run properly in a Python
344+
environment. Similar to the requirements listed in the `[build-system]`
345+
table above:
364346

365347
```toml
366348
[build-system] # <- this is a table
@@ -426,7 +408,6 @@ maintainers = [
426408
]
427409
readme = "README.md"
428410
license = "MIT"
429-
requires-python = ">=3.10"
430411

431412
dependencies = ["numpy>=1.0", "requests==10.1", "pandas", "pydantic>=1.7,<2"]
432413
```
@@ -455,7 +436,7 @@ pydantic^1.10
455436
One build tool that you should be aware of that pins dependencies to an upper bound by default is Poetry. [Read more about how to safely add dependencies with Poetry, here.](challenges-with-poetry)
456437
:::
457438

458-
### Step 5: Add PyPI classifiers
439+
### Step 4: Add PyPI classifiers
459440

460441
Next you will add classifiers to your `pyproject.toml` file. The value for each classifier that you add to your `pyproject.toml` file must come from the list of [PyPI accepted classifier values found here](https://PyPI.org/classifiers/). Any deviations in spelling and format will cause issues when you publish to PyPI.
461442

@@ -500,7 +481,6 @@ maintainers = [
500481
]
501482
readme = "README.md"
502483
license = "MIT"
503-
requires-python = ">=3.10"
504484

505485
dependencies = ["numpy>=1.0", "requests==10.1", "pandas", "pydantic>=1.7,<2"]
506486

@@ -516,7 +496,7 @@ classifiers = [
516496

517497
Note that while classifiers are not required in your `pyproject.toml` file, they will help users find your package. As such we strongly recommend that you add them.
518498

519-
### Step 6: Add the `[project.urls]` table
499+
### Step 5: Add the `[project.urls]` table
520500

521501
Finally, add the project.urls table to your pyproject.toml file.
522502

@@ -549,7 +529,6 @@ maintainers = [
549529
]
550530
readme = "README.md"
551531
license = "MIT"
552-
requires-python = ">=3.10"
553532

554533
dependencies = ["numpy>=1.0", "requests==10.1", "pandas", "pydantic>=1.7,<2"]
555534

@@ -599,7 +578,6 @@ maintainers = [
599578
]
600579
readme = "README.md"
601580
license = "MIT"
602-
requires-python = ">=3.10"
603581

604582
dependencies = ["numpy>=1.0", "requests==10.1", "pandas", "pydantic>=1.7,<2"]
605583

@@ -672,7 +650,6 @@ classifiers = [
672650

673651
dependencies = ["numpy>=1.0", "requests==10.1", "pandas", "pydantic>=1.7,<2"]
674652
# This is the metadata that pip reads to understand what versions your package supports
675-
requires-python = ">=3.10"
676653
readme = "README.md"
677654

678655
# Pick your license using the license expression syntax specified here:

0 commit comments

Comments
 (0)