Skip to content

Commit

Permalink
Use PythonEnvironment API in uv venv (#4029)
Browse files Browse the repository at this point in the history
There's no reason to be reaching into the lower-level `find_interpreter`
manually here.
  • Loading branch information
zanieb authored Jun 5, 2024
1 parent b05a39c commit db3c36d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
4 changes: 3 additions & 1 deletion crates/uv-interpreter/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 11 additions & 14 deletions crates/uv/src/commands/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -121,18 +119,17 @@ async fn venv_impl(
cache: &Cache,
printer: Printer,
) -> miette::Result<ExitStatus> {
// 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() {
Expand Down

0 comments on commit db3c36d

Please sign in to comment.