Skip to content

Set PYO3_BASE_PYTHON to the stable base interpreter path#3216

Open
alex wants to merge 1 commit into
PyO3:mainfrom
alex:pyo3-base-python
Open

Set PYO3_BASE_PYTHON to the stable base interpreter path#3216
alex wants to merge 1 commit into
PyO3:mainfrom
alex:pyo3-base-python

Conversation

@alex

@alex alex commented Jun 7, 2026

Copy link
Copy Markdown
Member

When a project is built by a PEP 517 frontend with build isolation, the frontend creates a randomly-named temporary virtualenv and maturin points PYO3_PYTHON inside it. Because pyo3-build-config registers rerun-if-env-changed=PYO3_PYTHON, the ephemeral path defeats cargo's build cache and forces recompilation of the pyo3 crates on every build, even when the underlying interpreter is identical.

maturin now additionally exports PYO3_BASE_PYTHON pointing at the stable base interpreter path (sys._base_executable, as reported by the interpreter probe). pyo3 versions that support it (PyO3/pyo3#6114) prefer it over PYO3_PYTHON and don't trigger rebuilds when only PYO3_PYTHON changes, keeping the build cache warm across isolated builds. Older pyo3 versions simply ignore the variable, and PYO3_PYTHON is still set to the venv path for build scripts that rely on it (e.g. cryptography, pydantic-core).

The variable is only set when the reported base executable actually exists on disk, since it takes precedence over PYO3_PYTHON on the pyo3 side. This is orthogonal to the existing MATURIN_PEP517_USE_BASE_PYTHON opt-in, which swaps the interpreter entirely (and is unchanged).

Refs PyO3/pyo3#6113

🤖 Generated with Claude Code

When a project is built by a PEP 517 frontend with build isolation, the
frontend creates a randomly-named temporary virtualenv and maturin
points PYO3_PYTHON inside it. Because pyo3-build-config registers
rerun-if-env-changed=PYO3_PYTHON, the ephemeral path defeats cargo's
build cache and forces recompilation of the pyo3 crates on every build,
even when the underlying interpreter is identical.

maturin now additionally exports PYO3_BASE_PYTHON pointing at the
stable base interpreter path (sys._base_executable). pyo3 versions that
support it prefer it over PYO3_PYTHON and don't trigger rebuilds when
only PYO3_PYTHON changes, keeping the build cache warm. Older pyo3
versions simply ignore the variable, and PYO3_PYTHON is still set to
the venv path for build scripts that rely on it.

See PyO3/pyo3#6113

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants