267 words
1 minutes
006: πŸ“ Using a specific version of Python with uv

There are a few different ways to request a specific version of Python when using uv.

πŸ”– I like to use Python Documentation by Version to list the available version. Building on the last post about getting EOL info via the CLI, you could also use:

uv tool run --python 3.11.6 --from norwegianblue eol python

That will print the full version string for each minor release. (Depending on what exactly you’re trying to do, that might not be helpful.)

The --python 3.11.6 tells uv to use Python 3.11.6.

βœ… Check the Python version#

You can specify the Python version and check the Python version all in one go:

# uv run --python 3.12 python -V Python 3.12.8

Let’s say you wanted to get the path to that Python 3.12.8 executable. Enter uv python find.

# uv python find 3.12

πŸ“ If you have used a non-default location for UV_PYTHON_INSTALL_DIR, you’ll need to take that into account.

If you want to check with version of Python and get a path to the executable that uv would use by default, you can use uv python find 3.12.

πŸ“ Pinning the version in .python-version#

The presence of a .python-version file in the working directory (and any parent directories) can be used to pin a particular Python version. There is even a uv python pin command that can be used to generate said .python-version file. The docs note that the --python works with most uv commands. It looks like uv python pin is one of the commands where --python is not accepted.

# uv python pin 3.12 Pinned `.python-version` to `3.12` # cat .python-version 3.12

The --python flag takes precedence over .python-version, as I would expect.

🌞

006: πŸ“ Using a specific version of Python with uv
https://brie.ninja/posts/006/
Author
Brie Carranza
Published at
2025-01-28