From dc4ccb067ffefe1e61c2cbc8447241b0409b755d Mon Sep 17 00:00:00 2001 From: trim21 Date: Wed, 1 Jul 2026 21:47:26 +0800 Subject: [PATCH 1/2] fix: check interpreter exists for VIRTUAL_ENV and CONDA_PREFIX When VIRTUAL_ENV or CONDA_PREFIX is set but points to a deleted or Python-less environment, pyo3-build-config would blindly use the derived interpreter path and fail with 'No such file or directory'. Now we check that the interpreter exists before using it, falling back to other detection methods with a warning if it does not. Fixes #6177 --- newsfragments/6177.fixed.md | 3 +++ pyo3-build-config/src/impl_.rs | 28 ++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 newsfragments/6177.fixed.md diff --git a/newsfragments/6177.fixed.md b/newsfragments/6177.fixed.md new file mode 100644 index 00000000000..087e6f6f350 --- /dev/null +++ b/newsfragments/6177.fixed.md @@ -0,0 +1,3 @@ +Fix build failure when `VIRTUAL_ENV` or `CONDA_PREFIX` is set but the Python interpreter +does not exist at the derived path. PyO3 now checks that the interpreter exists and +falls back to other detection methods with a warning if it does not. diff --git a/pyo3-build-config/src/impl_.rs b/pyo3-build-config/src/impl_.rs index 0a706fcdceb..11b13bedec1 100644 --- a/pyo3-build-config/src/impl_.rs +++ b/pyo3-build-config/src/impl_.rs @@ -2471,8 +2471,32 @@ fn get_env_interpreter() -> Option { match (env_var("VIRTUAL_ENV"), env_var("CONDA_PREFIX")) { // Use cfg rather than CARGO_CFG_TARGET_OS because this affects where files are located on the // build host - (Some(dir), None) => Some(venv_interpreter(&dir, cfg!(windows))), - (None, Some(dir)) => Some(conda_env_interpreter(&dir, cfg!(windows))), + (Some(dir), None) => { + let path = venv_interpreter(&dir, cfg!(windows)); + if path.exists() { + Some(path) + } else { + warn!( + "VIRTUAL_ENV is set to `{}` but no Python interpreter found at `{}`; ignoring", + dir.to_string_lossy(), + path.display() + ); + None + } + }, + (None, Some(dir)) => { + let path = conda_env_interpreter(&dir, cfg!(windows)); + if path.exists() { + Some(path) + } else { + warn!( + "CONDA_PREFIX is set to `{}` but no Python interpreter found at `{}`; ignoring", + dir.to_string_lossy(), + path.display() + ); + None + } + } (Some(_), Some(_)) => { warn!( "Both VIRTUAL_ENV and CONDA_PREFIX are set. PyO3 will ignore both of these for \ From 4c8770e28e7772b4da99077593699196c923f9d3 Mon Sep 17 00:00:00 2001 From: trim21 Date: Wed, 1 Jul 2026 22:04:28 +0800 Subject: [PATCH 2/2] rename newsfragment to match PR number, apply rustfmt --- newsfragments/{6177.fixed.md => 6178.fixed.md} | 0 pyo3-build-config/src/impl_.rs | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename newsfragments/{6177.fixed.md => 6178.fixed.md} (100%) diff --git a/newsfragments/6177.fixed.md b/newsfragments/6178.fixed.md similarity index 100% rename from newsfragments/6177.fixed.md rename to newsfragments/6178.fixed.md diff --git a/pyo3-build-config/src/impl_.rs b/pyo3-build-config/src/impl_.rs index 11b13bedec1..2c1bd243641 100644 --- a/pyo3-build-config/src/impl_.rs +++ b/pyo3-build-config/src/impl_.rs @@ -2483,7 +2483,7 @@ fn get_env_interpreter() -> Option { ); None } - }, + } (None, Some(dir)) => { let path = conda_env_interpreter(&dir, cfg!(windows)); if path.exists() {