Skip to content

fix: check interpreter exists for VIRTUAL_ENV and CONDA_PREFIX#6178

Open
trim21 wants to merge 2 commits into
PyO3:mainfrom
trim21:fix-6177-env-interpreter-check
Open

fix: check interpreter exists for VIRTUAL_ENV and CONDA_PREFIX#6178
trim21 wants to merge 2 commits into
PyO3:mainfrom
trim21:fix-6177-env-interpreter-check

Conversation

@trim21

@trim21 trim21 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #6177.

When VIRTUAL_ENV or CONDA_PREFIX is set but points to a deleted or Python-less environment, pyo3-build-config would blindly use the derived interpreter path and fail with No such file or directory.

Now we check that the interpreter exists before using it, falling back to other detection methods with a warning if it does not.

trim21 added 2 commits July 1, 2026 21:47
When VIRTUAL_ENV or CONDA_PREFIX is set but points to a deleted or
Python-less environment, pyo3-build-config would blindly use the
derived interpreter path and fail with 'No such file or directory'.

Now we check that the interpreter exists before using it, falling
back to other detection methods with a warning if it does not.

Fixes PyO3#6177

@davidhewitt davidhewitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. I'm not convinced that this relaxation is a good idea; it seems to me like this makes it easier for users to accidentally misconfigure their system (e.g. typo in `VIRTUAL_ENV)

The warn! system from build scripts is sadly quite limited, it is not visible to users unless the build fails. In this case it's quite likely that some other python will be found and probably built against, so the misconfiguration could easily be silent.

On balance, I think it's preferable for PyO3 to continue to be strict here, and in the (hopefully rare) cases where PyO3 is attempting to use an invalid interpreter we give users the right nudge.

See suggestion below

Comment on lines +2476 to +2485
if path.exists() {
Some(path)
} else {
warn!(
"VIRTUAL_ENV is set to `{}` but no Python interpreter found at `{}`; ignoring",
dir.to_string_lossy(),
path.display()
);
None
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we do it this way, then the build should almost immediately fail to configure, users will see the warning, and they can decide what to do differently.

Suggested change
if path.exists() {
Some(path)
} else {
warn!(
"VIRTUAL_ENV is set to `{}` but no Python interpreter found at `{}`; ignoring",
dir.to_string_lossy(),
path.display()
);
None
}
if !path.exists() {
warn!(
"VIRTUAL_ENV is set to `{}` but no Python interpreter found at `{}`",
dir.to_string_lossy(),
path.display()
);
}
Some(path)

Same for CONDA_PREFIX below.

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.

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

2 participants