Files
uv/docs/concepts/python-versions.md
T

150 lines
4.9 KiB
Markdown
Raw Normal View History

# Python versions
2024-06-26 12:28:42 -04:00
A Python version is composed of a Python interpreter (i.e. the `python` executable), the standard
library, and other supporting files. It is common for an operating system to come with a Python
version installed.
2024-06-26 12:28:42 -04:00
## Requesting a version
2024-06-26 12:28:42 -04:00
uv will automatically download a Python version if it cannot be found.
2024-06-26 12:28:42 -04:00
2024-07-22 13:15:11 -04:00
For example, when creating a virtual environment:
2024-06-26 12:28:42 -04:00
```bash
2024-07-22 13:15:11 -04:00
uv venv --python 3.11.6
2024-06-26 12:28:42 -04:00
```
uv will ensure that Python 3.11.6 is available — downloading and installing it if necessary — then
create the virtual environment with it.
2024-06-26 12:28:42 -04:00
Many Python version request formats are supported:
2024-06-26 12:28:42 -04:00
- `<version>` e.g. `3`, `3.12`, `3.12.3`
- `<version-specifier>` e.g. `>=3.12,<3.13`
- `<implementation>` e.g. `cpython` or `cp`
- `<implementation>@<version>` e.g. `cpython@3.12`
- `<implementation><version>` e.g. `cpython3.12` or `cp312`
- `<implementation><version-specifier>` e.g. `cpython>=3.12,<3.13`
- `<implementation>-<version>-<os>-<arch>-<libc>` e.g. `cpython-3.12.3-macos-aarch64-none`
## Installing a Python version
2024-06-26 12:28:42 -04:00
Sometimes it is preferable to install the Python versions before they are needed.
2024-06-26 12:28:42 -04:00
2024-07-24 03:58:04 +08:00
uv bundles a list of downloadable CPython and PyPy distributions for macOS, Linux, and Windows.
To install a Python version at a specific version:
2024-06-26 12:28:42 -04:00
```bash
2024-07-03 08:44:29 -04:00
uv python install 3.12.3
2024-06-26 12:28:42 -04:00
```
To install the latest patch version:
```bash
2024-07-03 08:44:29 -04:00
uv python install 3.12
2024-06-26 12:28:42 -04:00
```
To install a version that satisfies constraints:
```bash
2024-07-03 08:44:29 -04:00
uv python install '>=3.8,<3.10'
2024-06-26 12:28:42 -04:00
```
To install multiple versions:
```bash
2024-07-03 08:44:29 -04:00
uv python install 3.9 3.10 3.11
2024-06-26 12:28:42 -04:00
```
## Project Python versions
2024-06-26 12:28:42 -04:00
By default `uv python install` will verify that a managed Python version is installed or install the
latest version.
2024-06-26 12:28:42 -04:00
However, a project may define a `.python-version` file specifying the default Python version to be
used. If present, uv will install the Python version listed in the file.
2024-06-26 12:28:42 -04:00
Alternatively, a project that requires multiple Python versions may also define a `.python-versions`
file. If present, uv will install all of the Python versions listed in the file. This file takes
precedence over the `.python-version` file.
2024-06-26 12:28:42 -04:00
uv will also respect Python requirements defined in a `pyproject.toml` file during project command
invocations.
2024-06-26 12:28:42 -04:00
## Viewing available Python versions
2024-06-26 12:28:42 -04:00
To list installed and available Python versions:
2024-06-26 12:28:42 -04:00
```bash
2024-07-03 08:44:29 -04:00
uv python list
2024-06-26 12:28:42 -04:00
```
By default, downloads for other platforms and old patch versions are hidden.
To view all versions:
```bash
2024-07-03 08:44:29 -04:00
uv python list --all-versions
2024-06-26 12:28:42 -04:00
```
To view Python versions for other platforms:
2024-06-26 12:28:42 -04:00
```bash
2024-07-03 08:44:29 -04:00
uv python list --all-platforms
2024-06-26 12:28:42 -04:00
```
To exclude downloads and only show installed Python versions:
2024-06-26 12:28:42 -04:00
```bash
2024-07-03 08:44:29 -04:00
uv python list --only-installed
2024-06-26 12:28:42 -04:00
```
## Adjusting Python version preferences
2024-06-26 12:28:42 -04:00
By default, uv will attempt to use Python versions found on the system and only download managed
interpreters when necessary. However, It's possible to adjust uv's Python version selection
preference with the `python-preference` option.
2024-06-26 12:28:42 -04:00
- `only-managed`: Only use managed Python installations; never use system Python installations
- `installed`: Prefer installed Python installations, only download managed Python installations
if no system Python installation is found
- `managed`: Prefer managed Python installations over system Python installations, even if
fetching is required
- `system`: Prefer system Python installations over managed Python installations
- `only-system`: Only use system Python installations; never use managed Python installations
2024-06-26 12:28:42 -04:00
These options allow disabling uv's managed Python versions entirely or always using them and
ignoring any existing system installations.
## Discovery order
When searching for a Python version, the following locations are checked:
- Managed Python versions in the `UV_PYTHON_INSTALL_DIR`.
- A Python interpreter on the `PATH` as `python3` on macOS and Linux, or `python.exe` on Windows.
- On Windows, the Python interpreter returned by `py --list-paths` that matches the requested
version.
If a specific Python version is requested, e.g. `--python 3.7`, additional executable names are
included in the search:
- A Python interpreter on the `PATH` as, e.g., `python3.7` on macOS and Linux.
2024-07-22 13:15:11 -04:00
## Python distributions
Python does not publish official distributable CPython binaries, uv uses third-party standalone
distributions from the
[`python-build-standalone`](https://github.com/indygreg/python-build-standalone) project. The
project is partially maintained by the uv maintainers and is used by many other Python projects.
2024-07-22 13:15:11 -04:00
The Python distributions are self-contained and highly-portable. Additionally, these distributions
have various build-time optimizations enabled to ensure they are performant.
2024-07-22 13:15:11 -04:00
These distributions have some behavior quirks, generally as a consequence of portability. See the
[`python-build-standalone`
quirks](https://gregoryszorc.com/docs/python-build-standalone/main/quirks.html) documentation for
details.
2024-07-24 03:58:04 +08:00
PyPy distributions are provided by the PyPy project.