tracel-ai/burn · error

error running python interpreter

Error message

error running python interpreter

What it means

When `LIBTORCH_USE_PYTORCH` is set, burn-tch's build script asks the Python interpreter (from `PYTHON_SYS_EXECUTABLE` or `python`) to print its PyTorch/libtorch details and `.expect("error running python interpreter")` panics if the process cannot be spawned or its output cannot be captured. This means the Python executable was not found or failed to execute — not that PyTorch is missing from its output. Build of the burn-tch crate aborts.

Source

Thrown at crates/burn-tch/build.rs:74

        // https://github.com/PyO3/maturin/blob/243b8ec91d07113f97a6fe74d9b2dcb88086e0eb/src/target.rs#L547
        let python_interpreter = match os {
            Os::Windows => PathBuf::from("python.exe"),
            Os::Linux | Os::Macos => {
                if env::var_os("VIRTUAL_ENV").is_some() {
                    PathBuf::from("python")
                } else {
                    PathBuf::from("python3")
                }
            }
        };
        let mut libtorch_include_dirs = vec![];
        let mut libtorch_lib_dir = None;
        let cxx11_abi = if env_var_rerun("LIBTORCH_USE_PYTORCH").is_ok() {
            let output = std::process::Command::new(&python_interpreter)
                .arg("-c")
                .arg(PYTHON_PRINT_PYTORCH_DETAILS)
                .output()
                .expect("error running python interpreter");
            let mut cxx11_abi = None;
            for line in String::from_utf8_lossy(&output.stdout).lines() {
                match line.strip_prefix("LIBTORCH_CXX11: ") {
                    Some("True") => cxx11_abi = Some("1".to_owned()),
                    Some("False") => cxx11_abi = Some("0".to_owned()),
                    _ => {}
                }
                if let Some(path) = line.strip_prefix("LIBTORCH_INCLUDE: ") {
                    libtorch_include_dirs.push(PathBuf::from(path))
                }
                if let Some(path) = line.strip_prefix("LIBTORCH_LIB: ") {
                    libtorch_lib_dir = Some(PathBuf::from(path))
                }
            }
            match cxx11_abi {
                Some(cxx11_abi) => cxx11_abi,
                None => panic!("no cxx11 abi returned by python {output:?}"),
            }

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Install Python and ensure `python` (or `python3`) is on PATH: `python -c "import torch"` must work in the same shell that runs cargo
  2. Set `PYTHON_SYS_EXECUTABLE=/path/to/python` explicitly to the interpreter that has PyTorch installed
  3. Verify `LIBTORCH_USE_PYTORCH` is intentional — if you meant to link a downloaded libtorch instead, unset it and set `LIBTORCH_DIR`/`LIBTORCH_BYPASS_VERSION_CHECK` appropriately
  4. Check the interpreter is executable and matches the platform (permissions, shebang, Windows/WSL path confusion)

Example fix

// before (shell, building with LIBTORCH_USE_PYTORCH=1 but no python on PATH)
cargo build -p burn-tch   # panics: error running python interpreter
// after
export PYTHON_SYS_EXECUTABLE=$(which python3)
python3 -c 'import torch; print(torch.__version__)'  # sanity check
cargo build -p burn-tch
Defensive patterns

Strategy: validation

Validate before calling

# run in the same shell/environment as cargo build before setting LIBTORCH_USE_PYTORCH
python -c 'import torch; import os; print(torch.__version__, torch.__file__)' || echo 'python+torch unavailable'
[ -n "$PYTHON_SYS_EXECUTABLE" ] && [ -x "$PYTHON_SYS_EXECUTABLE" ] || which python

Try / catch

# build-script panic; verify the interpreter spawn works before building:
"${PYTHON_SYS_EXECUTABLE:-python}" -c "import torch" || { echo 'python interpreter cannot run torch'; exit 1; }

Prevention

When it happens

Trigger: Setting `LIBTORCH_USE_PYTORCH=1` while building burn-tch when: no `python`/`python3` is on PATH, `PYTHON_SYS_EXECUTABLE` points to a nonexistent file, the interpreter is not executable (permissions), or spawn fails for OS/ABI reasons. The `.expect` fires at build.rs:74 on `Command::new(python).arg("-c")...output()`.

Common situations: Clean Docker/CI images without Python installed; venv deactivated so `python` resolves nowhere; pointing `PYTHON_SYS_EXECUTABLE` at a Windows `python.exe` path from WSL or vice versa; conda environments removed after being referenced.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/34f50b0f12fafadc. Report an issue: GitHub.