Invoke python -m venv from a different working directory#1698
Merged
Conversation
When `python -m venv` is run, after creating the virtual environment it invokes `ensurepip` to install pip. The `ensurepip` module in older Python versions didn't correctly run its Python subprocesses in isolated mode, meaning that the working directory is added to `sys.path`. This can cause issues if the app's build directory contains files/directories that shadow expected package names (such as a `brotli` directory). For example: ``` -----> Installing Poetry 1.8.4 Error: Command '['/tmp/codon/tmp/cache/.heroku/python-poetry/venv/bin/python', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1. ! Internal Error: Unable to create virtual environment for Poetry. ! ! The 'python -m venv' command to create a virtual environment did ! not exit successfully. ! ! See the log output above for more information. ``` The best fix is for apps to upgrade to newer Python patch versions (3.8.14+, 3.9.14+, 3.10.6+), since they include the upstream `ensurepip` fix as well as many other bug and security fixes. However, to ensure venv creation still works on these older Python versions, as a workaround we can run the `python -m venv` command from a different working directory (as an alternative to isolated mode). Fixes #1697. GUS-W-17215816.
68b70cd to
1fc7a92
Compare
runesoerensen
approved these changes
Nov 12, 2024
Merged
edmorley
added a commit
that referenced
this pull request
Feb 24, 2025
This is an alternative approach to installing Poetry that means we can skip installing pip into its virtual environment, but still support the outdated Python versions which bundle older pip (that don't support the `--python` option; see #1687) or that don't correctly isolate the environment when running `ensurepip` (see #1698). Skipping installing pip speeds up the cold cache build for Poetry slightly, and also reduces the build cache size (which will help with the cache save and restore times for warm builds too). The pip installed in the Poetry venv wasn't exposed to apps (since it wasn't on `PATH`) so is safe to remove. GUS-W-17895154.
edmorley
added a commit
that referenced
this pull request
Feb 24, 2025
This is an alternative approach to installing Poetry that means we can skip installing pip into its virtual environment, but still support the outdated Python versions which bundle older pip (that don't support the `--python` option; see #1687) or that don't correctly isolate the environment when running `ensurepip` (see #1698). Skipping installing pip speeds up the cold cache build for Poetry slightly, and also reduces the build cache size (which will help with the cache save and restore times for warm builds too). The pip installed in the Poetry venv wasn't exposed to apps (since it wasn't on `PATH`) so is safe to remove. GUS-W-17895154.
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.
When
python -m venvis run, it invokesensurepipto install pip after creating the virtual environment.However, the
ensurepipmodule in older Python versions didn't correctly run its Python subprocesses in isolated mode, meaning that the working directory is added tosys.path. This can cause issues if the app's build directory contains files/directories that shadow expected package names (such as abrotlidirectory).For example:
The best fix is for apps to upgrade to newer Python patch versions (3.8.14+, 3.9.14+, 3.10.6+), since they include the upstream
ensurepipfix as well as many other bug and security fixes.However, to ensure venv creation still works on these older Python versions, as a workaround we can run the
python -m venvcommand from a different working directory (as an alternative to isolated mode).Fixes #1697.
GUS-W-17215816.