Skip to content

Commit f679987

Browse files
authored
Include the parent interpreter in Python discovery when --system is used (#7440)
Closes #7417 Tested with this epic blurb ``` ❯ uv pip install --python /Users/zb/.local/share/uv/python/cpython-3.13.0rc2-macos-aarch64-none/bin/python3 . --break-system-packages --reinstall Resolved 1 package in 0.91ms Built uv @ file:///Users/zb/workspace/uv Prepared 1 package in 2m 48s Uninstalled 1 package in 1ms Installed 1 package in 1ms - uv==0.4.10 + uv==0.4.10 (from file:///Users/zb/workspace/uv) ❯ /Users/zb/.local/share/uv/python/cpython-3.13.0rc2-macos-aarch64-none/bin/python3 -m uv pip install --system -v httpx DEBUG uv 0.4.10 DEBUG Searching for Python interpreter in system path DEBUG Found `cpython-3.13.0rc2-macos-aarch64-none` at `/Users/zb/.local/share/uv/python/cpython-3.13.0rc2-macos-aarch64-none/bin/python3` (parent interpreter) DEBUG Using Python 3.13.0rc2 environment at /Users/zb/.local/share/uv/python/cpython-3.13.0rc2-macos-aarch64-none/bin/python3 ```
1 parent e9378be commit f679987

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

crates/uv-python/src/discovery.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,13 @@ pub enum Error {
211211
///
212212
/// The following sources are supported:
213213
///
214-
/// - Spawning interpreter (via `UV_INTERNAL__PARENT_INTERPRETER`)
215214
/// - Active virtual environment (via `VIRTUAL_ENV`)
216215
/// - Active conda environment (via `CONDA_PREFIX`)
217216
/// - Discovered virtual environment (e.g. `.venv` in a parent directory)
218217
///
219218
/// Notably, "system" environments are excluded. See [`python_executables_from_installed`].
220219
fn python_executables_from_environments<'a>(
221220
) -> impl Iterator<Item = Result<(PythonSource, PathBuf), Error>> + 'a {
222-
let from_parent_interpreter = std::iter::once_with(|| {
223-
std::env::var_os("UV_INTERNAL__PARENT_INTERPRETER")
224-
.into_iter()
225-
.map(|path| Ok((PythonSource::ParentInterpreter, PathBuf::from(path))))
226-
})
227-
.flatten();
228-
229221
let from_virtual_environment = std::iter::once_with(|| {
230222
virtualenv_from_env()
231223
.into_iter()
@@ -253,8 +245,7 @@ fn python_executables_from_environments<'a>(
253245
})
254246
.flatten_ok();
255247

256-
from_parent_interpreter
257-
.chain(from_virtual_environment)
248+
from_virtual_environment
258249
.chain(from_conda_environment)
259250
.chain(from_discovered_environment)
260251
}
@@ -383,15 +374,31 @@ fn python_executables<'a>(
383374
environments: EnvironmentPreference,
384375
preference: PythonPreference,
385376
) -> Box<dyn Iterator<Item = Result<(PythonSource, PathBuf), Error>> + 'a> {
377+
// Always read from `UV_INTERNAL__PARENT_INTERPRETER` — it could be a system interpreter
378+
let from_parent_interpreter = std::iter::once_with(|| {
379+
std::env::var_os("UV_INTERNAL__PARENT_INTERPRETER")
380+
.into_iter()
381+
.map(|path| Ok((PythonSource::ParentInterpreter, PathBuf::from(path))))
382+
})
383+
.flatten();
384+
386385
let from_environments = python_executables_from_environments();
387386
let from_installed = python_executables_from_installed(version, implementation, preference);
388387

388+
// Limit the search to the relevant environment preference; we later validate that they match
389+
// the preference but queries are expensive and we query less interpreters this way.
389390
match environments {
390-
EnvironmentPreference::OnlyVirtual => Box::new(from_environments),
391-
EnvironmentPreference::ExplicitSystem | EnvironmentPreference::Any => {
392-
Box::new(from_environments.chain(from_installed))
391+
EnvironmentPreference::OnlyVirtual => {
392+
Box::new(from_parent_interpreter.chain(from_environments))
393+
}
394+
EnvironmentPreference::ExplicitSystem | EnvironmentPreference::Any => Box::new(
395+
from_parent_interpreter
396+
.chain(from_environments)
397+
.chain(from_installed),
398+
),
399+
EnvironmentPreference::OnlySystem => {
400+
Box::new(from_parent_interpreter.chain(from_installed))
393401
}
394-
EnvironmentPreference::OnlySystem => Box::new(from_installed),
395402
}
396403
}
397404

crates/uv-python/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,8 +1247,8 @@ mod tests {
12471247
)??;
12481248
assert_eq!(
12491249
python.interpreter().python_full_version().to_string(),
1250-
"3.12.3",
1251-
"We should prefer the system interpreter"
1250+
"3.12.0",
1251+
"We should prefer the parent interpreter since it's not virtual"
12521252
);
12531253

12541254
// Test with `EnvironmentPreference::OnlyVirtual`

0 commit comments

Comments
 (0)