Repository navigation
Conversation
Closes #334, closes #161. Two paired bugs on ``python -m venv`` against a relenv onedir. They compose but are independent, with fixes in different files; both are addressed here with regression tests that run on Windows / macOS / Linux CI via ``verify-build-action.yml``. ## gh-334 (relenv/build/windows.py) ``python -m venv <target>`` fails on Windows 3.13+ with:: [WinError 2] The system cannot find the file specified Unable to copy 'Lib\venv\scripts\nt\venvlauncher.exe' to '<venv>\Scripts\python.exe' CPython 3.13 rewrote ``venv.EnvBuilder`` (upstream gh-99834) to look for the launchers exclusively at ``Lib/venv/scripts/nt/venv{w,}launcher.exe``, dropping the ``Scripts/``-adjacent fallback 3.10-3.12 used. Earlier relenv builds staged the launchers only in ``Scripts/`` and the ``finalize`` archive globs stripped the entire ``Lib/venv/scripts/`` subtree, so even once the launchers were in the right place the child venv would be missing its activation scripts. Two changes in ``build/windows.py``: * after ``shutil.copytree(Lib)`` also copy both launchers into ``Lib/venv/scripts/nt/`` * add ``/Lib/venv/scripts/*`` to the ``finalize`` tarball globs so ``activate``, ``activate.bat``, ``Activate.ps1``, ``activate.fish``, ``deactivate.bat`` survive archival ## gh-161 (relenv/runtime.py) ``python -m venv`` succeeds (on 3.10-3.12, or on 3.13+ after the gh-334 fix) but the child venv has no relenv runtime wiring: no ``RelenvImporter`` on ``sys.meta_path``, no OpenSSL FIPS provider ordering, no ``sysconfig`` wrapping, no cargo config. On FIPS-enabled hosts this surfaces as hashlib failures (the original report); off FIPS it still leaves the child quietly running without the hooks relenv installs. Root cause: relenv's base-interpreter runtime is wired up by a ``relenv.pth`` dropped into the base's ``site-packages/``. ``python -m venv`` creates a child with an empty ``site-packages/`` and no such ``.pth``, so ``bootstrap()`` never runs in the child. Fix via new ``wrap_venv()`` in ``relenv/runtime.py``, called from the existing chain of wrappers in ``bootstrap()``: monkey-patch ``venv.EnvBuilder.post_setup`` to drop a ``relenv.pth`` into every child venv it creates, baking the absolute path of this installation's ``runtime.py`` into the file so the child can import it without having a ``relenv`` package in its own ``site-packages``. Follows the project's existing design decision (commit ``9346e9e``, Mar 2023) to use ``.pth`` instead of ``sitecustomize``. Four defensive details: * Idempotency guard via ``EnvBuilder._relenv_wrapped`` -- same pattern used elsewhere in the module to survive ``importlib.reload(site)``. * ``ImportError`` from ``import venv`` is swallowed -- some stripped stdlib builds and the nox-spawned unit-test env under setup-python + virtualenv 21.15 land without ``venv``, where there's nothing to wrap. Matches ``wrapsitecustomize``'s posture. * Any exception inside the wrapped ``post_setup`` is caught and logged under ``RELENV_DEBUG`` so a quirky ``EnvBuilder`` subclass can never break a user's ``python -m venv``. * Skip the ``.pth`` drop when the venv is pip's isolated build-env (path contains ``pip-build-env``, pip's stable public prefix at ``pip/_internal/utils/temp_dir.py::tempdir_kinds.BUILD_ENV``). Without this, pip's wheel-build envs re-bootstrap the full relenv runtime -- hijacking ``sysconfig.get_config_var("BINDIR")`` to point at the base onedir and breaking wheel scheme computation, surfaced by ``test_pip_install_salt_pip_dir`` on macOS when salt builds from sdist. Mirrors the same ``"pip-build-env"`` marker that ``wrapsitecustomize`` already uses to skip its ``sys.path`` munging. Covers stdlib ``venv`` only. ``virtualenv``, ``uv venv``, ``poetry env`` use independent venv-creation paths and would need parallel hooks -- tracked as a follow-up on the issue. ## Regression tests (tests/test_verify_build.py) * ``test_venv_inherits_relenv_runtime`` (all platforms): creates a stdlib venv from the relenv onedir, asserts ``RelenvImporter`` is on the child's ``sys.meta_path``. Sanity-checks the base interp first so a broken ``.pth`` fails loud rather than being papered over by the venv check. * ``test_python_m_venv_windows`` (Windows-only, pairs with gh-334): asserts ``python -m venv`` succeeds, the resulting venv has both launcher executables and the full set of activation scripts, and the child's ``sys.base_prefix`` points back at the onedir. End-to-end verified on ``win-11`` SSH host before the first push: a baseline v0.23.1 onedir reproduces ``[WinError 2]``; the gh-334 patch alone unblocks ``python -m venv``; the gh-161 patch on top makes the child inherit ``RelenvImporter`` on ``sys.meta_path``.
dwoz
force-pushed
the
fix-python-m-venv-on-windows
branch
from
November 20, 2026 08:40
ee72f35 to
fc52584
Compare
1 of 2 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #334, closes #161.
Two paired bugs on
python -m venvagainst a relenv onedir. They compose but are independent, with fixes in different files; both are addressed here with regression tests that run on Windows CI.gh-334 (commit
5bcd3d3)python -m venv <target>fails on Windows 3.13+ with:CPython 3.13 rewrote
venv.EnvBuilder(upstream gh-99834) to look for the launchers exclusively atLib/venv/scripts/nt/venv{w,}launcher.exe, dropping theScripts/-adjacent fallback that 3.10-3.12 used. Earlier relenv builds staged the launchers only inScripts/and thefinalizearchive globs stripped the entireLib/venv/scripts/subtree, so even once the launchers were in the right place the child venv would be missing its activation scripts. Fix inrelenv/build/windows.py:shutil.copytree(Lib), also copy both launchers intoLib/venv/scripts/nt//Lib/venv/scripts/*to thefinalizetarball globs soactivate,activate.bat,Activate.ps1,activate.fish,deactivate.batsurvive archivalgh-161 (commit
e15b415)python -m venvsucceeds (on a 3.10–3.12 onedir, or on 3.13+ after the gh-334 fix), but the resulting child venv has no relenv runtime wiring: noRelenvImporteronsys.meta_path, no OpenSSL FIPS provider ordering, nosysconfigwrapping, no cargo config. On FIPS-enabled hosts this surfaces as hashlib failures (the original report); off FIPS it still leaves the child quietly running without the hooks relenv installs.Root cause: relenv's base-interpreter runtime is wired up by a
relenv.pthdropped into the base'ssite-packages/.python -m venvcreates a child with a fresh emptysite-packages/and no such.pth, sobootstrap()never runs in the child.Fix in
relenv/runtime.py: newwrap_venv()that monkey-patchesvenv.EnvBuilder.post_setupfrom insidebootstrap()to drop arelenv.pthinto every child venv it creates, baking the absolute path of this installation'sruntime.pyinto the file so the child can import it without having arelenvpackage in its ownsite-packages. Idempotency guard viaEnvBuilder._relenv_wrapped(same pattern used elsewhere in the module). Exceptions during post_setup are swallowed and logged underRELENV_DEBUGso a quirkyEnvBuildersubclass can never break the user'spython -m venv. Follows the project's existing design decision (commit9346e9e, Mar 2023) to use.pthinstead ofsitecustomize.Covers stdlib
venvonly.virtualenv,uv venv,poetry envuse their own venv-creation paths and would need parallel hooks — tracked as a follow-up.Regression tests (same commit as the gh-161 fix)
Two new tests in
tests/test_verify_build.py:test_venv_inherits_relenv_runtime(all platforms): creates a stdlib venv from the relenv onedir, assertsRelenvImporteris on the child'ssys.meta_path. Sanity-checks the base interp has it first so a broken.pthfails loud rather than being papered over by the venv check.test_python_m_venv_windows(Windows-only, pairs with Cannot create venv on Windows due to venvlauncher.exe and venvwlauncher.exe not being in expected folder #334): assertspython -m venvsucceeds, the resulting venv has both launcher executables (renamed topython.exe/pythonw.exebyEnvBuilder) and the full set of activation scripts (activate,activate.bat,Activate.ps1,deactivate.bat), and the child'ssys.base_prefixpoints back at the onedir.Test plan
win-11SSH host against the v0.23.1 onedir (v0.23.2 was still unpublished at the time of verification). Baseline reproduces[WinError 2]; Cannot create venv on Windows due to venvlauncher.exe and venvwlauncher.exe not being in expected folder #334 patch alone unblockspython -m venv; Relenv breaks virtualenvs (At least in FIPS enabled systems) #161 patch on top makes the child inheritRelenvImporteronsys.meta_path;relenv.pthis dropped into the child'ssite-packages/with the baked absolute path.verify-build-action.yml.