astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
18.94k stars 552 forks source link

`--python-preference only-system` uses managed interpreter if present in virtual environment #5144

Open konstin opened 1 month ago

konstin commented 1 month ago

When a venv from a managed toolchain exists, uv ignores --python-preference only-system.

To reproduce, take a system without python3.11 on path:

[project]
name = "foo"
version = "1"
requires-python = "==3.11.*"
dependencies = [
    "tqdm",
]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

Create a managed venv, then sync again with --python-preference only-system. The venv is still managed python, which we can confirm by trying to recreate the venv:

$ uv sync
Using Python 3.11.9 interpreter at: /home/konsti/.local/share/uv/python/cpython-3.11.9-linux-x86_64-gnu/bin/python3
Creating virtualenv at: .venv
Resolved 3 packages in 5ms
   Built foo @ file:///home/konsti/projects/foo
Prepared 1 package in 120ms
Installed 2 packages in 1ms
 + foo==1 (from file:///home/konsti/projects/foo)
 + tqdm==4.66.4
$ uv sync --python-preference only-system
Resolved 3 packages in 4ms
Audited 2 packages in 0.10ms
$ rm -r .venv
$ uv sync --python-preference only-system
error: No interpreter found for Python >=3.11, <3.12 in system path
zanieb commented 1 month ago

Can you share verbose and trace logs?

zanieb commented 1 month ago

Oh wait.. you're creating the virtual environment with the managed Python then requesting only-system in subsequent syncs? We don't perform discovery of an interpreter again since the environment already exists — this would only affect behavior if you also requested a new Python version.

I'm uncertain this should change.