Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
7 changes: 4 additions & 3 deletions crates/uv-python/src/virtualenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,14 @@ pub(crate) fn virtualenv_python_executable(venv: impl AsRef<Path>) -> PathBuf {
// If none of these exist, return the standard location
default_executable
} else {
// Check for both `python3` over `python`, preferring the more specific one
let default_executable = venv.join("bin").join("python3");
// Prefer the unversioned `python` command exposed by virtual environments.
let default_executable = venv.join("bin").join("python");
if default_executable.exists() {
return default_executable;
}

let executable = venv.join("bin").join("python");
// Fall back to `python3` for installations without an unversioned executable.
let executable = venv.join("bin").join("python3");
if executable.exists() {
return executable;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/uv/tests/pip_compile/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18511,8 +18511,8 @@ fn compile_broken_active_venv() -> Result<()> {
.arg("requirements.in"), @"
exit_code: 2 (failure)
----- stderr -----
error: Failed to inspect Python interpreter from active virtual environment at `.venv/bin/python3`
Caused by: Broken symlink at `.venv/bin/python3`, was the underlying Python interpreter removed?
error: Failed to inspect Python interpreter from active virtual environment at `.venv/bin/python`
Caused by: Broken symlink at `.venv/bin/python`, was the underlying Python interpreter removed?

hint: Consider recreating the environment (e.g., with `uv venv`)
");
Expand Down
4 changes: 2 additions & 2 deletions crates/uv/tests/pip_install/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14826,7 +14826,7 @@ fn strip_shebang_arguments() -> Result<()> {
insta::with_settings!({filters => context.filters()
}, {
insta::assert_snapshot!(script_content, @r#"
#![VENV]/bin/python3
#![VENV]/bin/python
# This is a test script with shebang arguments
import sys
print(f"Hello from {sys.executable}")
Expand All @@ -14840,7 +14840,7 @@ fn strip_shebang_arguments() -> Result<()> {
insta::with_settings!({filters => context.filters()
}, {
insta::assert_snapshot!(gui_script_content, @r#"
#![VENV]/bin/python3
#![VENV]/bin/python
# This is a test GUI script with shebang arguments
import sys
print(f"Hello from GUI script: {sys.executable}")
Expand Down
63 changes: 63 additions & 0 deletions crates/uv/tests/python/python_find.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(unix)]
use anyhow::Result;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::prelude::{FileTouch, PathChild};
use assert_fs::{fixture::FileWriteStr, prelude::PathCreateDir};
Expand Down Expand Up @@ -649,6 +651,67 @@ fn python_find_venv() {
}
}

#[test]
#[cfg(unix)]
fn python_find_venv_executable_precedence() -> Result<()> {
let context = uv_test::test_context!("3.12");
let python = context.interpreter();
let python3 = python.with_file_name("python3");

// Prefer `python` when discovering an environment or requesting it by directory.
uv_snapshot!(context.filters(), context.python_find(), @"
exit_code: 0 (success)
----- stdout -----
[VENV]/bin/python
");

uv_snapshot!(context.filters(), context.python_find().arg(context.venv.path()), @"
exit_code: 0 (success)
----- stdout -----
[VENV]/bin/python
");

uv_snapshot!(context.filters(), context.python_find()
.env(EnvVars::VIRTUAL_ENV, context.venv.path()), @"
exit_code: 0 (success)
----- stdout -----
[VENV]/bin/python
");

// An explicit executable path is still used as given.
uv_snapshot!(context.filters(), context.python_find().arg(&python3), @"
exit_code: 0 (success)
----- stdout -----
[VENV]/bin/python3
");

// Environments containing only `python` remain supported.
fs_err::remove_file(&python3)?;
uv_snapshot!(context.filters(), context.python_find().arg(context.venv.path()), @"
exit_code: 0 (success)
----- stdout -----
[VENV]/bin/python
");

// Keep `python3` independent of the `python` symlink before removing the latter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This comment explains (maybe not that clearly, but it could just be me) what the next two lines are trying to do. But then there are no more comments for the remaining snapshots. I feel they help a lot.

It could just be // Environments containing only python3 remain supported leading in for both with the blank line between them removed.

We actually don't test the case without .arg(context.venv.path()) for python above.

fs_err::os::unix::fs::symlink(fs_err::canonicalize(&python)?, &python3)?;
fs_err::remove_file(&python)?;

uv_snapshot!(context.filters(), context.python_find().arg(context.venv.path()), @"
exit_code: 0 (success)
----- stdout -----
[VENV]/bin/python3
");

uv_snapshot!(context.filters(), context.python_find(), @"
exit_code: 0 (success)
----- stdout -----
[VENV]/bin/python3
");

Ok(())
}

#[cfg(unix)]
#[test]
fn python_find_unsupported_version() {
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/sync/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9155,7 +9155,7 @@ fn sync_invalid_environment() -> Result<()> {
uv_snapshot!(context.filters(), context.sync(), @"
exit_code: 0 (success)
----- stderr -----
warning: Ignoring existing virtual environment linked to non-existent Python interpreter: .venv/[BIN]/[PYTHON] -> python
warning: Ignoring existing virtual environment linked to non-existent Python interpreter: .venv/[BIN]/[PYTHON] -> does-not-exist

@EliteTK EliteTK Sep 9, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Intriguing... The test looked more wrong before than it does now.

So if a symlink is pointing at a broken symlink we do not print the final broken target, but rather the intermediate (working) target?

Also weird that we don't print a full path?

I guess this avoids dealing with loops and such.

Oh well. Not important for this PR...

Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
Removed virtual environment at: .venv
Creating virtual environment at: .venv
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/tool/tool_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2801,7 +2801,7 @@ fn tool_install_force() {
}, {
// Should run black in the virtual environment
assert_snapshot!(fs_err::read_to_string(executable).unwrap(), @r#"
#![TEMP_DIR]/tools/black/bin/python3
#![TEMP_DIR]/tools/black/bin/python
# -*- coding: utf-8 -*-
import sys
from black import patched_main
Expand Down