PyO3 / PyO3/pyo3

pyo3-build-config: CONDA_PREFIX set but no valid Python interpreter causes build failure

Open Beginner friendly
#6,177 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

🐛 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:

  1. 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)
  2. Non-conda tools like pixi also set CONDA_PREFIX, and the environment might not contain Python at all
  3. 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
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

In pyo3-build-config/src/impl_.rs, start at get_env_interpreter and the CONDA_PREFIX branch around lines 2470-2483; inspect how conda_env_interpreter derives the path. Confirm that a missing interpreter is ignored with the proposed warning, while an existing interpreter remains selected, and exercise the cargo build scenario from the report.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
build-system
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.