Repository navigation
sysconfig data inconsistencies caused by the disabling the site initialization on virtual environments #126789
Copy link
Copy link
Closed
Labels
Description
Bug report
Bug description:
Currently, running the Python interpreter with -S (disabling the site module initialization) causes several inconsistencies sysconfig.get_paths() depends on whether sysconfig was imported before or after the site initialization
Currently, sysconfig calculates the paths at import time and caches them. This results in it resulting incorrect paths if the site initialization happens after sysconfig was imported.
$ python -S
Python 3.14.0a1+ experimental free-threading build (heads/main:de0d5c6e2e1, Oct 23 2024, 15:37:46) [GCC 14.2.1 20240910] on linux
>>> import sysconfig
>>> sysconfig.get_paths()
{'stdlib': '/usr/local/lib/python3.14t', 'platstdlib': '/usr/local/lib/python3.14t', 'purelib': '/usr/local/lib/python3.14t/site-packages', 'platlib': '/usr/local/lib/python3.14t/site-packages', 'include': '/usr/local/include/python3.14td', 'platinclude': '/usr/local/include/python3.14td', 'scripts': '/usr/local/bin', 'data': '/usr/local'}
>>> import site
>>> site.main()
>>> sysconfig.get_paths()
{'stdlib': '/usr/local/lib/python3.14t', 'platstdlib': '/usr/local/lib/python3.14t', 'purelib': '/usr/local/lib/python3.14t/site-packages', 'platlib': '/usr/local/lib/python3.14t/site-packages', 'include': '/usr/local/include/python3.14td', 'platinclude': '/usr/local/include/python3.14td', 'scripts': '/usr/local/bin', 'data': '/usr/local'}Expected output when the site module has been initialized:
$ python
Python 3.14.0a1+ experimental free-threading build (heads/main:de0d5c6e2e1, Oct 23 2024, 15:37:46) [GCC 14.2.1 20240910] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sysconfig
>>> sysconfig.get_paths()
{'stdlib': '/usr/local/lib/python3.14t', 'platstdlib': '/home/anubis/.virtualenvs/test-sysconfig-paths/lib/python3.14t', 'purelib': '/home/anubis/.virtualenvs/test-sysconfig-paths/lib/python3.14t/site-packages', 'platlib': '/home/anubis/.virtualenvs/test-sysconfig-paths/lib/python3.14t/site-packages', 'include': '/usr/local/include/python3.14td', 'platinclude': '/usr/local/include/python3.14td', 'scripts': '/home/anubis/.virtualenvs/test-sysconfig-paths/bin', 'data': '/home/anubis/.virtualenvs/test-sysconfig-paths'}And just to make sure this is not dependent on the interpreter being run with -S, and rather on the sysconfig import time.
$ python -S
Python 3.14.0a1+ experimental free-threading build (heads/main:de0d5c6e2e1, Oct 23 2024, 15:37:46) [GCC 14.2.1 20240910] on linux
>>> import site
>>> site.main()
>>> import sysconfig
>>> sysconfig.get_paths()
{'stdlib': '/usr/local/lib/python3.14t', 'platstdlib': '/home/anubis/.virtualenvs/test-sysconfig-paths/lib/python3.14t', 'purelib': '/home/anubis/.virtualenvs/test-sysconfig-paths/lib/python3.14t/site-packages', 'platlib': '/home/anubis/.virtualenvs/test-sysconfig-paths/lib/python3.14t/site-packages', 'include': '/usr/local/include/python3.14td', 'platinclude': '/usr/local/include/python3.14td', 'scripts': '/home/anubis/.virtualenvs/test-sysconfig-paths/bin', 'data': '/home/anubis/.virtualenvs/test-sysconfig-paths'}CPython versions tested on:
3.9, 3.10, 3.11, 3.12, 3.13, 3.14
Operating systems tested on:
Linux
Linked PRs
- GH-126789: fix some sysconfig data on late site initializations #126812
- [3.13] GH-126789: fix some sysconfig data on late site initializations #126918
- [3.12] GH-126789: fix some sysconfig data on late site initializations #126919
- gh-126789: Correct sysconfig test exclusions for iOS and Android. #126941
- [3.13] gh-126789: Correct sysconfig test exclusions for iOS and Android. (GH-126941) #126950
- [3.12] gh-126789: Correct sysconfig test exclusions for iOS and Android. (GH-126941) #126959
- GH-126789: fix some sysconfig data on late site initializations #127729
Reactions are currently unavailable
Activity
FFY00 commentedon Nov 13, 2024
Things are a bit messy, as the virtual environment mechanism is technically part of the
sitecustomization. In reality, thepyvenv.cfgdetection is implemented on the interpreter initialization, but the customization itself is done bysite. I feel like this should probably be reconsidered.Anyway, to match this model, if
sitehas been initialized,sysconfig.get_paths()should always return the virtual environment paths. So, the correct result should be as follows:FFY00 commentedon Nov 13, 2024
Correction:
sysconfigdoes not cache thesysconfig.get_paths(), it cachessysconfig.get_config_vars(), which is used to calculate the paths.pythonGH-126789: fix some sysconfig data on late site initializations
[-]Fix `sysconfig.get_paths()` inconsistencies caused by the disabling the `site` initialization on virtual environments[/-][+]`sysconfig` data inconsistencies caused by the disabling the `site` initialization on virtual environments[/+]FFY00 commentedon Nov 13, 2024
While fixing the main bug, I have noticed that the
prefixandexec_prefixvariables get overwritten by_init_posix, so on virtual environmentssysconfig.get_config_vars('prefix')andsys.prefixwill have different values, the same goes forexec_prefix.IMO this is worth to fix, but we probably shouldn't backport it as it is a significant behavior change, and there's a possibility it would break existing user code.
FFY00 commentedon Nov 13, 2024
Just some more background on this,
sysconfig.get_paths()uses an incomplete copy of the config vars directory to expand the path placeholder, which is what threw me off initially. This makes it a little bit more tricky to fix the issue, as we need to make sure we update the config vars cache without creating a recursive loop, but wasn't too difficult to get the implementation right.GH-126789: fix some sysconfig data on late site initializations
pythonGH-126789: fix some sysconfig data on late site initializations
pythonGH-126789: fix some sysconfig data on late site initializations
[3.12] GH-126789: fix some sysconfig data on late site initializations
[3.13] GH-126789: fix some sysconfig data on late site initializations
pythonGH-126789: fix some sysconfig data on late site initializations
5 remaining items
[3.13] gh-126789: Correct sysconfig test exclusions for iOS and Andro…
Revert "pythonGH-126789: fix some sysconfig data on late site initial…
fixup! Revert "pythonGH-126789: fix some sysconfig data on late site …
[3.12] gh-126789: Correct sysconfig test exclusions for iOS and Andro…
FFY00 commentedon Nov 20, 2024
@hugovk, sorry I just say this now. I should have mentioned it in the issue text, but as per the title this only affects virtual environments.
The issue is that currently
siteis responsible for the virtual environment initialization (settingsys.prefixandsys.exec_prefix), so when you run a virtual environment with-S,sys.prefixandsys.exec_prefixpoint to the base installation, meaning that, canonically, you are not in a virtual environment. Insite.main(),sys.prefixandsys.exec_prefixare updated to point to the virtual environment.If the
sysconfig.get_config_vars()cache, which is used when calculatingsysconfig.get_paths(), is created before thesiteinitialization, it still holds the old prefix values, resulting insysconfigreporting different paths depending on whether the cache was generated before or after thesiteinitialization.The issue only affects virtual environments, because that's the only case where
sys.prefixandsys.exec_prefixchange after initialization.I hope that helps clarify things. Let me know if you have any more questions.
hugovk commentedon Nov 20, 2024
Thanks for the clarification!
thesamesam commentedon Dec 6, 2024
The change for this seems to have regressed building Firefox with Python 3.12.8 and Python 3.13.1: https://bugzilla.mozilla.org/show_bug.cgi?id=1935621
I don't know enough about Firefox's build system, mach, to know if it is an issue on their side or not.
FFY00 commentedon Dec 7, 2024
@thesamesam, I investigated the issue, the source of the issue is that they are overwriting
sys.prefixand were relying onsysconfigdata using the original prefix. GH-126812 fixedsysconfigdata not matchingsys.prefix/sys.exec_prefixif they were updated after import.sysconfigused to cache their values at import, so if they were updated afterwards, the data wouldn't reflect the change.In GH-126987, we moved the virtual environment activation from
siteto the interpreter path initialization, which means that the reproducible above would no longer trigger thesysconfigcache issue, assys.prefix/sys.exec_prefixare no longer updated by thesitemodule. Because of this, I also reverted the cache bugfix, but seeing that some folks are updatingsys.prefixmanually, I think we should re-apply the bugfix.pythonGH-126789: fix some sysconfig data on late site initializations
GH-126789: fix some sysconfig data on late site initializations (#127729
[firefox] 132.0.2-3: fix build with python 3.13
[firefox] 132.0.2-3: fix build with python 3.13
pythonGH-126789: fix some sysconfig data on late site initializations (…
pythonGH-126789: fix some sysconfig data on late site initializations
pythongh-126789: Correct sysconfig test exclusions for iOS and Androi…