From 14a1e7a1a0c45b72fa7a7f44d758cdcafb2adcf6 Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Tue, 12 May 2026 12:43:38 +0330 Subject: [PATCH 1/4] Compare pyenv.cfg version field with sys.version_info and emit a RuntimeWarning on minor-version mismatch. --- Lib/site.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Lib/site.py b/Lib/site.py index cb1108dbaf1f81..e6f6a19290c09c 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -796,6 +796,7 @@ def venv(known_paths): if candidate_conf: virtual_conf = candidate_conf system_site = "true" + version = None # Issue 25185: Use UTF-8, as that's what the venv module uses when # writing the file. with open(virtual_conf, encoding='utf-8') as f: @@ -808,6 +809,28 @@ def venv(known_paths): system_site = value.lower() elif key == 'home': sys._home = value + elif key == 'version': + version = value + + 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, + ) if sys.prefix != site_prefix: _warn(f'Unexpected value in sys.prefix, expected {site_prefix}, got {sys.prefix}', RuntimeWarning) From 1bca0c8aa0962f02a10524c7bd24ff8ffc79d45a Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Tue, 12 May 2026 12:49:47 +0330 Subject: [PATCH 2/4] Add news entry --- .../2026-05-12-12-49-30.gh-issue-127727.hNmj9G.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-05-12-12-49-30.gh-issue-127727.hNmj9G.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-05-12-12-49-30.gh-issue-127727.hNmj9G.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-12-12-49-30.gh-issue-127727.hNmj9G.rst new file mode 100644 index 00000000000000..474ae434f6885e --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-05-12-12-49-30.gh-issue-127727.hNmj9G.rst @@ -0,0 +1,3 @@ +Warn when running a virtual environment created for a different minor Python +version than the current interpreter, and suggest using ``python -m venv +--upgrade``. From 5d0238f7454d6ee33a591afadc331b48c54f96cd Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Tue, 12 May 2026 15:11:42 +0330 Subject: [PATCH 3/4] Fix linting issues --- Lib/site.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/site.py b/Lib/site.py index e6f6a19290c09c..b83b3d1252d4a6 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -811,7 +811,7 @@ def venv(known_paths): sys._home = value elif key == 'version': version = value - + if version: try: major, minor = map(int, version.split(".")[:2]) From 64a8f28ae29bc77b90fa5bc33f832e4c5ecfba71 Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Tue, 12 May 2026 15:13:32 +0330 Subject: [PATCH 4/4] Fix linting --- Lib/site.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/site.py b/Lib/site.py index b83b3d1252d4a6..9ccc218d191070 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -817,7 +817,6 @@ def venv(known_paths): major, minor = map(int, version.split(".")[:2]) except ValueError: major, minor = None - if ( major == sys.version_info.major and minor is not None