Skip to content

Commit bcbfae0

Browse files
committed
Include the parent interpreter in Python discovery when --system is used
1 parent 23494d8 commit bcbfae0

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

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

0 commit comments

Comments
 (0)