Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke python -m venv from a different working directory #1698

Merged
merged 1 commit into from
Nov 12, 2024

Conversation

edmorley
Copy link
Member

@edmorley edmorley commented Nov 12, 2024

When python -m venv is run, it invokes ensurepip to install pip after creating the virtual environment.

However, 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.

@edmorley edmorley added the bug label Nov 12, 2024
@edmorley edmorley self-assigned this Nov 12, 2024
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.
@edmorley edmorley marked this pull request as ready for review November 12, 2024 18:04
@edmorley edmorley requested a review from a team as a code owner November 12, 2024 18:04
@edmorley edmorley enabled auto-merge (squash) November 12, 2024 18:05
@edmorley edmorley merged commit 498e2d2 into main Nov 12, 2024
7 checks passed
@edmorley edmorley deleted the workaround-ensurepip-bug branch November 12, 2024 18:14
@heroku-linguist heroku-linguist bot mentioned this pull request Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ensurepip error during Poetry venv creation when using outdated Python versions
2 participants