Skip to content

gh-127727: Warn when running a virtual environment created for a different minor Python version#149715

Open
sepehr-rs wants to merge 8 commits into
python:mainfrom
sepehr-rs:fix-127727
Open

gh-127727: Warn when running a virtual environment created for a different minor Python version#149715
sepehr-rs wants to merge 8 commits into
python:mainfrom
sepehr-rs:fix-127727

Conversation

@sepehr-rs
Copy link
Copy Markdown

@sepehr-rs sepehr-rs commented May 12, 2026

@sepehr-rs sepehr-rs requested review from FFY00 and warsaw as code owners May 12, 2026 09:25
@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented May 12, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@sepehr-rs
Copy link
Copy Markdown
Author

I’m seeing the “Lint / lint” check fail in CI, but I can’t find a concrete error message in the job logs, and make patchcheck passes locally.
I may be missing something obvious, any pointers would be appreciated.

@picnixz
Copy link
Copy Markdown
Member

picnixz commented May 12, 2026

There are lines with whitespaces only but the GH UI may not show them properly.

@sepehr-rs
Copy link
Copy Markdown
Author

Thanks a lot, lint check is passing now!

Copy link
Copy Markdown
Member

@FFY00 FFY00 left a comment

Choose a reason for hiding this comment

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

Thanks @sepehr-rs!

The PR mostly looks okay. I have some questions inline, and the following suggestions:

  • Also check version_info, in addition to version
  • Raise a warning on a malformed version or version_info fields
  • Add test(s)

Comment thread Lib/site.py Outdated
Comment on lines +815 to +832
if version:
try:
major, minor = map(int, version.split(".")[:2])
except ValueError:
major, minor = None
if (
major == sys.version_info.major
and minor is not None
and minor != sys.version_info.minor
and not hasattr(sys, "_venv_version_warning_emitted")
):
_warn(
f"This virtual environment was created for Python {major}.{minor}, "
f"but the current interpreter is Python "
f"{sys.version_info.major}.{sys.version_info.minor}. "
"Consider running `python -m venv --upgrade` to update the environment.",
RuntimeWarning,
)
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.

  • Why does the warning only run when the major version is the same?
  • Where does sys._venv_version_warning_emitted come from? Did you forget to include code?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Why does the warning only run when the major version is the same?

I focused on minor version mismatches since that’s what the issue described. Major version differences (2.x vs 3.x) typically fail more obviously, so I thought a warning would be redundant there. Happy to add it if you think it’s useful though!

Where does sys._venv_version_warning_emitted come from? Did you forget to include code?

Yes, sorry about that! That was leftover from local testing and shouldn’t have been included. I’ve removed it.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 19, 2026

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@sepehr-rs sepehr-rs requested a review from vsajip as a code owner May 21, 2026 12:05
@sepehr-rs
Copy link
Copy Markdown
Author

sepehr-rs commented May 21, 2026

Hi @FFY00! Thank you for your kind review and suggestions.
I have made the requested changes; please review again

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 21, 2026

Thanks for making the requested changes!

@FFY00: please review the changes made to this pull request.

@bedevere-app bedevere-app Bot requested a review from FFY00 May 21, 2026 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn when running in a virtual environment that was created for a different minor Python version

3 participants