PyO3/pyo3 · error
target_triple_from_env() must be called from a build script
Error message
target_triple_from_env() must be called from a build script
What it means
Panic in target_triple_from_env: the TARGET environment variable, which only Cargo sets when running a build script, is absent because std::env::var("TARGET") failed. The function is a build-script helper; calling it from a normal binary, test, or before Cargo exports TARGET hits this expect.
Source
Thrown at pyo3-build-config/src/impl_.rs:72
/// Gets an external environment variable, and registers the build script to rerun if
/// the variable changes.
pub fn env_var(var: &str) -> Option<OsString> {
println!("cargo:rerun-if-env-changed={var}");
#[cfg(test)]
{
READ_ENV_VARS.with(|env_vars| {
env_vars.borrow_mut().push(var.to_owned());
});
}
env::var_os(var)
}
/// Gets the compilation target triple from environment variables set by Cargo.
///
/// Must be called from a crate build script.
pub fn target_triple_from_env() -> Triple {
env::var("TARGET")
.expect("target_triple_from_env() must be called from a build script")
.parse()
.expect("Unrecognized TARGET environment variable value")
}
fn sanitize_stable_abi_version(
stable_abi_version: Option<PythonVersion>,
version: PythonVersion,
) -> Result<PythonVersion> {
if let Some(min_version) = stable_abi_version {
ensure!(
min_version <= version,
"cannot set a minimum Python version {} higher than the interpreter version {} \
(the minimum Python version is implied by the abi3-py3{} feature)",
min_version,
version,
min_version.minor
);
Ok(min_version)View on GitHub (pinned to ac9b6899d3)
Solutions
- Call target_triple_from_env only from a crate's build.rs
- If invoked outside Cargo, set TARGET manually to the desired triple (e.g. x86_64-unknown-linux-gnu)
- Use the target_triple crate or restructure the code to receive the triple as a parameter
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at pyo3-build-config/src/impl_.rs:72 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of PyO3/pyo3@ac9b6899d3 (2026-09-05).
Data as JSON: /api/errors/6c01d2831bf9f53b.
Report an issue: GitHub.