Skip to content

Fix PIPENV_ACTIVE not being unset on deactivate#6462

Merged
matteius merged 2 commits intomainfrom
fix-pipenv-active-deactivate
Dec 10, 2025
Merged

Fix PIPENV_ACTIVE not being unset on deactivate#6462
matteius merged 2 commits intomainfrom
fix-pipenv-active-deactivate

Conversation

@matteius
Copy link
Copy Markdown
Member

Summary

Fixes #6220

When using pipenv shell with pexpect-based compat mode, the deactivate function from the virtualenv's activate script doesn't know to unset the PIPENV_ACTIVE environment variable that pipenv sets. This causes subsequent pipenv shell commands to fail with:

Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.
New shell not activated to avoid nested environments.

Solution

This fix adds a wrapper around the deactivate function after sourcing the activate script. The wrapper:

  1. Saves the original deactivate function
  2. Defines a new deactivate function that calls the original and then unsets PIPENV_ACTIVE

Support is added for multiple shells:

  • bash/zsh: Uses declare -f to copy function definition
  • fish: Uses functions -c to copy and set -e to unset
  • csh: Uses alias and unsetenv
  • sh: Simple function wrapper with unset
  • xonsh: Python-like syntax
  • PowerShell: Uses $function: and Remove-Item Env:

Testing

Added unit test test_deactivate_wrapper_script_includes_unset_pipenv_active to verify the wrapper scripts include the appropriate unset command for each shell type.

Notes

This fix applies to the fork_compat mode (pexpect-based shell). The fork mode (fancy mode) doesn't source the activate script at all, so there's no deactivate function to wrap in that case.


Pull Request opened by Augment Code with guidance from the PR author

Adds a new section explaining when to use --system (single-app containers)
vs. virtual environments (containers with multiple Python apps or system tools).
Updates the best practices summary to reflect this nuanced guidance.

Closes #6438
When using 'pipenv shell' with pexpect-based compat mode, the deactivate
function from the virtualenv's activate script doesn't know to unset the
PIPENV_ACTIVE environment variable that pipenv sets. This causes
subsequent 'pipenv shell' commands to fail with 'Shell for
UNKNOWN_VIRTUAL_ENVIRONMENT already activated' error.

This fix adds a wrapper around the deactivate function after sourcing
the activate script. The wrapper calls the original deactivate function
and then unsets PIPENV_ACTIVE. Support is added for bash, zsh, fish,
csh, sh, xonsh, and PowerShell.

Fixes #6220
@matteius matteius merged commit c05edc9 into main Dec 10, 2025
14 of 20 checks passed
@matteius matteius deleted the fix-pipenv-active-deactivate branch December 10, 2025 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.

1 participant