🐛 Bug Report
pyo3-build-config blindly trusts CONDA_PREFIX to locate the Python interpreter, without verifying the interpreter actually exists at the derived path.
Environment
- OS: Linux
- pyo3 version: 0.27.2
- Rust: stable
💥 Reproducing
# Set CONDA_PREFIX to a non-existent or deleted environment
export CONDA_PREFIX=/nonexistent/path
# VIRTUAL_ENV is not set
cargo build # fails
Error:
error: failed to run the Python interpreter at /nonexistent/path/bin/python:
No such file or directory (os error 2)
🔍 Root Cause
In pyo3-build-config/src/impl_.rs:
fn get_env_interpreter() -> Option<PathBuf> {
match (env_var("VIRTUAL_ENV"), env_var("CONDA_PREFIX")) {
(Some(dir), None) => Some(venv_interpreter(&dir, cfg!(windows))),
(None, Some(dir)) => Some(conda_env_interpreter(&dir, cfg!(windows))),
// ...
}
}
conda_env_interpreter constructs $CONDA_PREFIX/bin/python without checking if the file actually exists.
This is problematic because CONDA_PREFIX can be set in situations where no valid Python interpreter is present:
- A conda/pixi environment was deleted but the shell still carries the env var (e.g., VSCode launched from a shell with the env active, then the env was cleaned up)
- Non-conda tools like pixi also set
CONDA_PREFIX, and the environment might not contain Python at all
- Leftover env vars from a previous session
✅ Proposed Fix
get_env_interpreter should verify that the derived interpreter path exists before returning it:
(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, path.display()
);
None
}
}
🐛 Bug Report
pyo3-build-configblindly trustsCONDA_PREFIXto locate the Python interpreter, without verifying the interpreter actually exists at the derived path.Environment
💥 Reproducing
Error:
🔍 Root Cause
In
pyo3-build-config/src/impl_.rs:conda_env_interpreterconstructs$CONDA_PREFIX/bin/pythonwithout checking if the file actually exists.This is problematic because
CONDA_PREFIXcan be set in situations where no valid Python interpreter is present:CONDA_PREFIX, and the environment might not contain Python at all✅ Proposed Fix
get_env_interpretershould verify that the derived interpreter path exists before returning it: