From 38051f52f88b1e46febeab0a9ae2b1da7847a233 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Tue, 4 Jun 2024 18:29:47 -0500 Subject: [PATCH] Use `PythonEnvironment` API in `uv venv` --- crates/uv-interpreter/src/environment.rs | 4 +++- crates/uv/src/commands/venv.rs | 25 +++++++++++------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/crates/uv-interpreter/src/environment.rs b/crates/uv-interpreter/src/environment.rs index 7257cb1e7139..e26b766000f6 100644 --- a/crates/uv-interpreter/src/environment.rs +++ b/crates/uv-interpreter/src/environment.rs @@ -26,7 +26,9 @@ struct PythonEnvironmentShared { } impl PythonEnvironment { - /// Create a [`PythonEnvironment`] from a user request. + /// Find a [`PythonEnvironment`]. + /// + /// This is the standard interface for discovering a Python environment for use with uv. pub fn find( python: Option<&str>, system: SystemPython, diff --git a/crates/uv/src/commands/venv.rs b/crates/uv/src/commands/venv.rs index 5e13f5fc263c..af01d9603968 100644 --- a/crates/uv/src/commands/venv.rs +++ b/crates/uv/src/commands/venv.rs @@ -20,9 +20,7 @@ use uv_configuration::{ConfigSettings, IndexStrategy, NoBinary, NoBuild, SetupPy use uv_dispatch::BuildDispatch; use uv_fs::Simplified; use uv_git::GitResolver; -use uv_interpreter::{ - find_default_interpreter, find_interpreter, InterpreterRequest, SourceSelector, -}; +use uv_interpreter::{PythonEnvironment, SystemPython}; use uv_resolver::{ExcludeNewer, FlatIndex, InMemoryIndex, OptionsBuilder}; use uv_types::{BuildContext, BuildIsolation, HashStrategy, InFlight}; @@ -121,18 +119,17 @@ async fn venv_impl( cache: &Cache, printer: Printer, ) -> miette::Result { - // Locate the Python interpreter. - let interpreter = if let Some(python) = python_request.as_ref() { - let system = uv_interpreter::SystemPython::Required; - let request = InterpreterRequest::parse(python); - let sources = SourceSelector::from_settings(system, preview); - find_interpreter(&request, system, &sources, cache) + // Locate the Python interpreter to use in the environment + // If a specific interpreter is requested, it is required to come from the system. + // Otherwise, we'll allow the interpeter from a virtual environment to be used. + let system = if python_request.is_some() { + SystemPython::Required } else { - find_default_interpreter(preview, cache) - } - .into_diagnostic()? - .into_diagnostic()? - .into_interpreter(); + SystemPython::Allowed + }; + let interpreter = PythonEnvironment::find(python_request, system, preview, cache) + .into_diagnostic()? + .into_interpreter(); // Add all authenticated sources to the cache. for url in index_locations.urls() {