Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include the parent interpreter in Python discovery when --system is used #7440

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,13 @@ pub enum Error {
///
/// The following sources are supported:
///
/// - Spawning interpreter (via `UV_INTERNAL__PARENT_INTERPRETER`)
/// - Active virtual environment (via `VIRTUAL_ENV`)
/// - Active conda environment (via `CONDA_PREFIX`)
/// - Discovered virtual environment (e.g. `.venv` in a parent directory)
///
/// Notably, "system" environments are excluded. See [`python_executables_from_installed`].
fn python_executables_from_environments<'a>(
) -> impl Iterator<Item = Result<(PythonSource, PathBuf), Error>> + 'a {
let from_parent_interpreter = std::iter::once_with(|| {
std::env::var_os("UV_INTERNAL__PARENT_INTERPRETER")
.into_iter()
.map(|path| Ok((PythonSource::ParentInterpreter, PathBuf::from(path))))
})
.flatten();

let from_virtual_environment = std::iter::once_with(|| {
virtualenv_from_env()
.into_iter()
Expand Down Expand Up @@ -253,8 +245,7 @@ fn python_executables_from_environments<'a>(
})
.flatten_ok();

from_parent_interpreter
.chain(from_virtual_environment)
from_virtual_environment
.chain(from_conda_environment)
.chain(from_discovered_environment)
}
Expand Down Expand Up @@ -383,15 +374,31 @@ fn python_executables<'a>(
environments: EnvironmentPreference,
preference: PythonPreference,
) -> Box<dyn Iterator<Item = Result<(PythonSource, PathBuf), Error>> + 'a> {
// Always read from `UV_INTERNAL__PARENT_INTERPRETER` — it could be a system interpreter
let from_parent_interpreter = std::iter::once_with(|| {
std::env::var_os("UV_INTERNAL__PARENT_INTERPRETER")
.into_iter()
.map(|path| Ok((PythonSource::ParentInterpreter, PathBuf::from(path))))
})
.flatten();

let from_environments = python_executables_from_environments();
let from_installed = python_executables_from_installed(version, implementation, preference);

// Limit the search to the relevant environment preference; we later validate that they match
// the preference but queries are expensive and we query less interpreters this way.
match environments {
EnvironmentPreference::OnlyVirtual => Box::new(from_environments),
EnvironmentPreference::ExplicitSystem | EnvironmentPreference::Any => {
Box::new(from_environments.chain(from_installed))
EnvironmentPreference::OnlyVirtual => {
Box::new(from_parent_interpreter.chain(from_environments))
}
EnvironmentPreference::ExplicitSystem | EnvironmentPreference::Any => Box::new(
from_parent_interpreter
.chain(from_environments)
.chain(from_installed),
),
EnvironmentPreference::OnlySystem => {
Box::new(from_parent_interpreter.chain(from_installed))
}
EnvironmentPreference::OnlySystem => Box::new(from_installed),
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/uv-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,8 +1247,8 @@ mod tests {
)??;
assert_eq!(
python.interpreter().python_full_version().to_string(),
"3.12.3",
"We should prefer the system interpreter"
"3.12.0",
"We should prefer the parent interpreter since it's not virtual"
Comment on lines -1250 to +1251
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test case added in #7439

);

// Test with `EnvironmentPreference::OnlyVirtual`
Expand Down
Loading