Skip to content

Commit 14a1e7a

Browse files
committed
Compare pyenv.cfg version field with sys.version_info and emit a RuntimeWarning on
minor-version mismatch.
1 parent 54a5fd4 commit 14a1e7a

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Lib/site.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ def venv(known_paths):
796796
if candidate_conf:
797797
virtual_conf = candidate_conf
798798
system_site = "true"
799+
version = None
799800
# Issue 25185: Use UTF-8, as that's what the venv module uses when
800801
# writing the file.
801802
with open(virtual_conf, encoding='utf-8') as f:
@@ -808,6 +809,28 @@ def venv(known_paths):
808809
system_site = value.lower()
809810
elif key == 'home':
810811
sys._home = value
812+
elif key == 'version':
813+
version = value
814+
815+
if version:
816+
try:
817+
major, minor = map(int, version.split(".")[:2])
818+
except ValueError:
819+
major, minor = None
820+
821+
if (
822+
major == sys.version_info.major
823+
and minor is not None
824+
and minor != sys.version_info.minor
825+
and not hasattr(sys, "_venv_version_warning_emitted")
826+
):
827+
_warn(
828+
f"This virtual environment was created for Python {major}.{minor}, "
829+
f"but the current interpreter is Python "
830+
f"{sys.version_info.major}.{sys.version_info.minor}. "
831+
"Consider running `python -m venv --upgrade` to update the environment.",
832+
RuntimeWarning,
833+
)
811834

812835
if sys.prefix != site_prefix:
813836
_warn(f'Unexpected value in sys.prefix, expected {site_prefix}, got {sys.prefix}', RuntimeWarning)

0 commit comments

Comments
 (0)