Use python from python -m uv as default

Python tools run with `python -m <tool>` will use this `python` as their default python, including pip, virtualenv and the built-in venv. Calling Python tools this way is common, the [pip docs](https://pip.pypa.io/en/stable/user_guide/) use `python -m pip` exclusively, the built-in venv can only be called this way and certain project layouts require `python -m pytest` over `pytest`. This python interpreter takes precedence over the currently active (v)env.

These tools are all written in python and read `sys.executable`. To emulate this, we're setting `UV_DEFAULT_PYTHON` in the python module-launcher shim and read it in the default python discovery, prioritizing it over the active venv. User can also set `UV_DEFAULT_PYTHON` for their own purposes.

The test covers only half of the feature since we don't build the python package before running the tests.

Fixes #2058
Fixes #2222
This commit is contained in:
konstin 2024-03-10 14:35:57 +01:00
parent 7964bfbb2b
commit d8845dc444
13 changed files with 124 additions and 30 deletions

View file

@ -120,8 +120,8 @@ async fn resolve(
let flat_index = FlatIndex::default();
let index = InMemoryIndex::default();
// TODO(konstin): Should we also use the bootstrapped pythons here?
let real_interpreter =
find_default_python(&Cache::temp().unwrap()).expect("Expected a python to be installed");
let real_interpreter = find_default_python(&Cache::temp().unwrap(), false)
.expect("Expected a python to be installed");
let interpreter = Interpreter::artificial(real_interpreter.platform().clone(), markers.clone());
let build_context = DummyContext::new(Cache::temp()?, interpreter.clone());
let resolver = Resolver::new(