diff --git a/newsfragments/6178.fixed.md b/newsfragments/6178.fixed.md new file mode 100644 index 00000000000..087e6f6f350 --- /dev/null +++ b/newsfragments/6178.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..2c1bd243641 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 \