Skip to content

Commit db3c36d

Browse files
authored
Use PythonEnvironment API in uv venv (#4029)
There's no reason to be reaching into the lower-level `find_interpreter` manually here.
1 parent b05a39c commit db3c36d

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

crates/uv-interpreter/src/environment.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ struct PythonEnvironmentShared {
2626
}
2727

2828
impl PythonEnvironment {
29-
/// Create a [`PythonEnvironment`] from a user request.
29+
/// Find a [`PythonEnvironment`].
30+
///
31+
/// This is the standard interface for discovering a Python environment for use with uv.
3032
pub fn find(
3133
python: Option<&str>,
3234
system: SystemPython,

crates/uv/src/commands/venv.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ use uv_configuration::{ConfigSettings, IndexStrategy, NoBinary, NoBuild, SetupPy
2020
use uv_dispatch::BuildDispatch;
2121
use uv_fs::Simplified;
2222
use uv_git::GitResolver;
23-
use uv_interpreter::{
24-
find_default_interpreter, find_interpreter, InterpreterRequest, SourceSelector,
25-
};
23+
use uv_interpreter::{PythonEnvironment, SystemPython};
2624
use uv_resolver::{ExcludeNewer, FlatIndex, InMemoryIndex, OptionsBuilder};
2725
use uv_types::{BuildContext, BuildIsolation, HashStrategy, InFlight};
2826

@@ -121,18 +119,17 @@ async fn venv_impl(
121119
cache: &Cache,
122120
printer: Printer,
123121
) -> miette::Result<ExitStatus> {
124-
// Locate the Python interpreter.
125-
let interpreter = if let Some(python) = python_request.as_ref() {
126-
let system = uv_interpreter::SystemPython::Required;
127-
let request = InterpreterRequest::parse(python);
128-
let sources = SourceSelector::from_settings(system, preview);
129-
find_interpreter(&request, system, &sources, cache)
122+
// Locate the Python interpreter to use in the environment
123+
// If a specific interpreter is requested, it is required to come from the system.
124+
// Otherwise, we'll allow the interpeter from a virtual environment to be used.
125+
let system = if python_request.is_some() {
126+
SystemPython::Required
130127
} else {
131-
find_default_interpreter(preview, cache)
132-
}
133-
.into_diagnostic()?
134-
.into_diagnostic()?
135-
.into_interpreter();
128+
SystemPython::Allowed
129+
};
130+
let interpreter = PythonEnvironment::find(python_request, system, preview, cache)
131+
.into_diagnostic()?
132+
.into_interpreter();
136133

137134
// Add all authenticated sources to the cache.
138135
for url in index_locations.urls() {

0 commit comments

Comments
 (0)