Skip to content

Commit

Permalink
Use base executable to set virtualenv Python path
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Oct 22, 2024
1 parent 5a15e45 commit 75daf3a
Showing 1 changed file with 5 additions and 26 deletions.
31 changes: 5 additions & 26 deletions crates/uv-virtualenv/src/virtualenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,11 @@ pub(crate) fn create(
// considered the "base" for the virtual environment. This is typically the Python executable
// from the [`Interpreter`]; however, if the interpreter is a virtual environment itself, then
// the base Python executable is the Python executable of the interpreter's base interpreter.
let base_python = if cfg!(unix) {
// On Unix, follow symlinks to resolve the base interpreter, since the Python executable in
// a virtual environment is a symlink to the base interpreter.
uv_fs::canonicalize_executable(interpreter.sys_executable())?
} else if cfg!(windows) {
// On Windows, follow `virtualenv`. If we're in a virtual environment, use
// `sys._base_executable` if it exists; if not, use `sys.base_prefix`. For example, with
// Python installed from the Windows Store, `sys.base_prefix` is slightly "incorrect".
//
// If we're _not_ in a virtual environment, use the interpreter's executable, since it's
// already a "system Python". We canonicalize the path to ensure that it's real and
// consistent, though we don't expect any symlinks on Windows.
if interpreter.is_virtualenv() {
if let Some(base_executable) = interpreter.sys_base_executable() {
base_executable.to_path_buf()
} else {
// Assume `python.exe`, though the exact executable name is never used (below) on
// Windows, only its parent directory.
interpreter.sys_base_prefix().join("python.exe")
}
} else {
interpreter.sys_executable().to_path_buf()
}
} else {
unimplemented!("Only Windows and Unix are supported")
};
let base_python = std::path::absolute(
interpreter
.sys_base_executable()
.unwrap_or(interpreter.sys_executable()),
)?;

// Validate the existing location.
match location.metadata() {
Expand Down

0 comments on commit 75daf3a

Please sign in to comment.