Skip to content

Work around Pipenv bug when using --system#1842

Merged
edmorley merged 1 commit into
mainfrom
pipenv-workaround-system-bug
Jul 23, 2025
Merged

Work around Pipenv bug when using --system#1842
edmorley merged 1 commit into
mainfrom
pipenv-workaround-system-bug

Conversation

@edmorley

@edmorley edmorley commented Jul 23, 2025

Copy link
Copy Markdown
Member

After moving Pipenv to its own virtual environment in #1840 (to fix leaking Pipenv's dependencies into the app environment), apps that explicitly depend on a package that happen to also be a Pipenv dependency were seeing ModuleNotFoundError failures.

For example:

ModuleNotFoundError: No module named 'certifi'

Or:

ModuleNotFoundError: No module named 'packaging'

It appears that Pipenv's --system feature has a bug whereby it sees its own dependencies (installed in its own environment) as already installed (even though they aren't actually installed in the app environment), so incorrectly skips installing them.

In general, it seems Pipenv's support for --system is pretty buggy (for example, it doesn't work with pipenv run either):
https://github.com/pypa/pipenv/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22--system%20flag%22

The classic Python buildpack doesn't currently use virtual environments (since it installs a dedicated Python install for the app, that's already separate from system Python), and switching to them would be a significant change that may cause other types of breakage (for example, to apps that hardcode the path to Python).

As such for now, we need to continue to use pipenv install --system (as the buildpack has done for some time), but I have found the bug can be worked around if we set VIRTUAL_ENV to the Python install location, to trick Pipenv into thinking it's operating in a virtual environment.

The Pipenv tests have been updated to confirm that an explicit certifi install now works.

GUS-W-19118553.

@edmorley edmorley self-assigned this Jul 23, 2025
@edmorley edmorley added the bug label Jul 23, 2025
After moving Pipenv to its own virtual environment in #1840 (to fix
leaking Pipenv's dependencies into the app environment), apps that
explicitly depend on a package that happen to also be a Pipenv
dependency were seeing `ModuleNotFoundError` failures.

For example:

```
ModuleNotFoundError: No module named 'certifi'
```

It appears that Pipenv's `--system` feature has a bug whereby it sees
its own dependencies (installed in its own environment) as already
installed (even though they aren't actually installed in the app
environment), so incorrectly skips installing them.

In general, it seems Pipenv's support for `--system` is pretty buggy:
https://github.com/pypa/pipenv/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22--system%20flag%22
(for example, it doesn't work with `pipenv run` either)

The classic Python buildpack doesn't currently use virtual environments
(since it installs a dedicated Python install for the app, that's
already separate from system Python), and switching to them would
be a significant change that may cause other types of breakage
(for example, to apps that hardcode the path to Python).

As such for now, we need to continue to use `pipenv install --system`
(as the buildpack has done for some time), but I have found the bug
can be worked around if we set `VIRTUAL_ENV` to the Python install
location, to trick Pipenv into thinking it's operating in a virtual
environment.

The Pipenv tests have been updated to confirm that an explicit
`certifi` install now works.

GUS-W-19118553.
@edmorley edmorley force-pushed the pipenv-workaround-system-bug branch from 9bee985 to a8b2fdc Compare July 23, 2025 16:22
@edmorley edmorley marked this pull request as ready for review July 23, 2025 16:40
@edmorley edmorley requested a review from a team as a code owner July 23, 2025 16:40
@edmorley edmorley merged commit 0bb90f7 into main Jul 23, 2025
11 of 14 checks passed
@edmorley edmorley deleted the pipenv-workaround-system-bug branch July 23, 2025 17:03
@heroku-linguist heroku-linguist Bot mentioned this pull request Jul 23, 2025
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.

2 participants