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

Detect python version from python project by default in uv venv #5592

Merged
Merged
Changes from 1 commit
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
29 changes: 27 additions & 2 deletions crates/uv/src/commands/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ use uv_dispatch::BuildDispatch;
use uv_fs::Simplified;
use uv_python::{
request_from_version_file, EnvironmentPreference, PythonFetch, PythonInstallation,
PythonPreference, PythonRequest,
PythonPreference, PythonRequest, VersionRequest,
};
use uv_resolver::{ExcludeNewer, FlatIndex};
use uv_resolver::{ExcludeNewer, FlatIndex, RequiresPython};
use uv_shell::Shell;
use uv_types::{BuildContext, BuildIsolation, HashStrategy};
use uv_warnings::warn_user_once;
use uv_workspace::{DiscoveryOptions, VirtualProject, WorkspaceError};

use crate::commands::project::find_requires_python;
use crate::commands::reporters::PythonDownloadReporter;
use crate::commands::{pip, ExitStatus, SharedState};
use crate::printer::Printer;
Expand Down Expand Up @@ -142,6 +144,29 @@ async fn venv_impl(
if preview.is_enabled() && interpreter_request.is_none() {
interpreter_request = request_from_version_file().await.into_diagnostic()?;
}
if preview.is_enabled() && interpreter_request.is_none() {
let project = match VirtualProject::discover(
&std::env::current_dir().into_diagnostic()?,
&DiscoveryOptions::default(),
)
.await
{
Ok(project) => Some(project),
Err(WorkspaceError::MissingPyprojectToml) => None,
Err(WorkspaceError::NonWorkspace(_)) => None,
Err(err) => return Err(err).into_diagnostic(),
};

if let Some(project) = project {
interpreter_request = find_requires_python(project.workspace())
.into_diagnostic()?
.as_ref()
.map(RequiresPython::specifiers)
.map(|specifiers| {
PythonRequest::Version(VersionRequest::Range(specifiers.clone()))
});
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Code here is adopted from run.rs:

match VirtualProject::discover(&CWD, &DiscoveryOptions::default()).await {
Ok(project) => Some(project),
Err(WorkspaceError::MissingPyprojectToml) => None,
Err(WorkspaceError::NonWorkspace(_)) => None,
Err(err) => return Err(err.into()),
}
};

and FoundInterpreter in mod.rs:
let requires_python = find_requires_python(workspace)?;
// (1) Explicit request from user
let python_request = if let Some(request) = python_request {
Some(request)
// (2) Request from `.python-version`
} else if let Some(request) = request_from_version_file(workspace.install_path()).await? {
Some(request)
// (3) `Requires-Python` in `pyproject.toml`
} else {
requires_python
.as_ref()
.map(RequiresPython::specifiers)
.map(|specifiers| PythonRequest::Version(VersionRequest::Range(specifiers.clone())))
};

}
if preview.is_disabled() && relocatable {
warn_user_once!("`--relocatable` is experimental and may change without warning");
}
Expand Down
Loading