Sub topic 4
Choosing and pinning a Python version
This chapter assumes you have run uv init on a project, you know what pyproject.toml contains, and you have used virtual environments before. Here you will learn how uv decides which Python interpreter a project uses. You will also learn how to control that choice and how to fix the errors people hit most often.
Two settings with different jobs
A uv project states its Python version in two places. They look similar but answer different questions.
| Setting | Where | Question it answers | Who reads it |
|---|---|---|---|
| `requires-python` | `[project]` table in `pyproject.toml` | Which versions can run this code? | uv's resolver, build tools, installers, and anyone who installs your package |
| `.python-version` | A plain text file in the project root | Which version should I use on this machine right now? | uv when it creates the environment |
requires-python is a range, such as >=3.12. It is part of the package metadata, so it travels with your published package. .python-version is a single request, such as 3.12. It is a local development choice and is normally committed to version control so that the whole team uses the same interpreter.
The first command from the project example shows both in action:
uv init --package weather-cli
cd weather-cli
uv python pin 3.12
cat .python-versionuv init writes a requires-python value into pyproject.toml and a .python-version file. uv python pin 3.12 then rewrites .python-version to contain 3.12. It does not change requires-python.
How uv picks an interpreter
When you run uv run or uv sync, uv looks for a version request in this order and uses the first one it finds:
- The
--pythoncommand-line option, or theUV_PYTHONenvironment variable. - The
.python-versionfile, in the project directory or a parent directory. - The
requires-pythonrange, if neither of the above exists.
Whatever the request is, uv then checks it against requires-python. The result must satisfy that range.
Writing version requests
A request can be more or less specific. All of these are valid for uv python pin and --python:
uv python pin 3.12 # any 3.12.x patch release
uv python pin 3.12.4 # one exact patch release
uv python pin [email protected] # name the implementation
uv python pin [email protected] # use PyPy instead of CPython
uv run --python 3.13 python --versionA short request such as 3.12 lets each developer use whatever 3.12 patch release they have. An exact request such as 3.12.4 makes everyone use the same patch release. The cost is that uv must find or download that exact version.
If you want the short request recorded as the exact version currently installed, add --resolved:
uv python pin --resolved 3.12Running uv python pin with no argument prints the current pin.
Finding, installing, and downloading Pythons
uv can use interpreters already on your system and can also download its own. You can inspect and manage them directly:
uv python list # versions uv knows about, installed or available
uv python install 3.12 # download and install a managed Python
uv python find 3.12 # show the interpreter uv would useBy default uv prefers its own managed installations over system interpreters. If no installed interpreter matches, it downloads one automatically. You can change this behavior:
- Set
UV_PYTHON_DOWNLOADS=never, or pass--no-python-downloads, to forbid downloads. This is useful on locked-down build machines where you want a clear failure instead of a silent download. - Set
UV_PYTHON_PREFERENCE=system, or pass--python-preference system, to prefer system interpreters.
Choosing `requires-python` well
Your requires-python value should state the oldest version you actually test and support:
[project]
name = "weather-cli"
version = "0.1.0"
requires-python = ">=3.12"This range matters beyond your own code. The resolver builds uv.lock so that it works for every Python version in the range. If your range is wide, every dependency must have a release compatible with all of it. A lower bound that is too old can force the resolver to pick older package versions. A bound that is too new excludes users who could run your code.
Avoid an upper bound such as <3.14 unless you know your code breaks on that version. Upper bounds cannot be undone by users of your library, and they cause resolution failures in projects that depend on yours. A pin in .python-version is the better tool for "use this version for development".
Changing the version of an existing project
Suppose you want to move from 3.12 to 3.13. Change the pin and sync:
uv python pin 3.13
uv syncuv notices that the existing .venv was built with a different interpreter and recreates it. If you are also dropping support for 3.12, edit requires-python to >=3.13 and run uv lock so that the lockfile is resolved for the new range. Commit .python-version, pyproject.toml, and uv.lock together.
For a one-off test on another version, use --python without touching any files:
uv run --python 3.13 pytestThis runs the tests under 3.13 without changing the pin.
Common errors
- The pin is outside
requires-python. If.python-versionsays3.11andrequires-pythonis>=3.12, uv reports that the requested Python is incompatible with the project and refuses to continue. Fix the pin, or widenrequires-pythonif your code really supports 3.11. - A download fails or is blocked. In CI or behind a proxy, uv may not reach the download source. Install the interpreter in your image, or allow the network access. Do not rely on a system Python that happens to be the wrong version.
- Different results for different people. If the file says
3.12and teammates have different patch releases, behavior can differ slightly. Use--resolvedor an exact pin if that matters for your project. - Forgetting to commit
.python-version. Without it, uv falls back torequires-python, and each machine can choose a different interpreter within the range. - An old
.venvafter a change. If something odd happens after changing versions, runuv syncagain. If a stale environment persists, delete.venvand re-runuv sync. It is safe to delete because uv rebuilds it fromuv.lock.
Exercise
A project has requires-python = ">=3.11" and a .python-version file containing 3.10. You run uv run pytest. What happens, and what are two ways to fix it?
Show answer
uv reads the pin (3.10) as the interpreter request, finds that it does not satisfy requires-python, and stops with an incompatibility error. One fix is to change the pin, for example uv python pin 3.12. The other is to lower requires-python to >=3.10, but only if the code and its dependencies really support 3.10.
Exercise
Your team wants every developer and CI job to use exactly the same patch release of Python 3.12 and to fail rather than download an interpreter on the CI machine. Which commands or settings do you use?
Show answer
Pin an exact release with uv python pin 3.12.4 (or uv python pin --resolved 3.12 to record the installed patch release) and commit .python-version. On CI, set UV_PYTHON_DOWNLOADS=never (or pass --no-python-downloads) so uv fails if the required interpreter is not already installed.