How to enforce `uv` to use specific version?

Few days ago, in AIML cohort that I teach, one of the student faced a problem where some python package refused to install because it required python 3.10 or higher.

So I asked the student to install the latest python using

uv python install 3.14

But we kept getting the same error.

Turns out, uv uses system python by default. (Which was 3.8)

Here are the steps that ensure uv uses specific version for your project.

uv python pin 3.14

The command uv python pin is used to set a specific Python version for your project by creating a .python-version file. This helps ensure that the project consistently uses the specified Python version across different environments.

Now check which version is being used via this command:

uv run python --version

In our case, it would return 3.8 because the .venv was already created with System python. So we had to rm -rf .venv first.

Then uv python pin 3.14 itself is enough for uv to install pinned python version, if not then uv python install 3.14

Finally, if you want to create .venv with specific python version you can always remove existing .venv followed by uv venv --python 3.14 (But usually it is not required. uv automatically creates .venv if .python-version exists.)