Open bakemocho opened 3 months ago
Pipenv forks the shell and activates the virtualenv in the forked shell, so you can just exit the shell (e.g. by running exit
or Ctrl+d) instead of calling deactivate
.
If you still want to re-use the forked shell, you can use the --anyway
flag for pipenv shell
.
If we want to unset PIPENV_ACTIVE
, we would need to modify the deactivate
function which is generated by virtualenv
in the activation scripts for each shell type. But I'm not sure if we should go with this solution.
Thank you for your response.
I will start using exit
instead of deactivate
from now on, but it seems the issue still persists. I appreciate the advice and hope a good solution can be found.
Thanks again for your help.
1. Problem Summary:
The issue highlights a problem with reactivating a Pipenv-created virtual environment after it has been deactivated using the deactivate
command. When attempting to reactivate using pipenv shell
, users encounter an error stating:
Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.
New shell not activated to avoid nested environments.
This error occurs because the PIPENV_ACTIVE
environment variable remains set even after deactivation, leading Pipenv to believe the environment is still active.
2. Comments Analysis:
exit
to leave the forked shell is the recommended way to deactivate the environment. They also suggest using the --anyway
flag with pipenv shell
to force reactivation within the same shell. However, the user (bakemocho) confirms that the problem persists even when using exit
.deactivate
function generated by virtualenv
. This is because it would involve altering code outside Pipenv's control, potentially leading to unforeseen issues.3. Proposed Resolution:
Given the nature of the problem and the comments, the best approach is to follow Naofal-Helal's suggestion of using the --anyway
flag. However, we should improve the user experience by:
Pipenv environment for this project was previously activated in this shell.
Use the '--anyway' flag with 'pipenv shell' to reactivate.
exit
is the preferred way to deactivate a Pipenv environment, and that --anyway
should be used for reactivation within the same shell.4. Code Changes:
a) Modify the error message in pipenv/environments.py
:
def is_in_virtualenv():
#... (existing code) ...
return virtual_env and not (pipenv_active or ignore_virtualenvs)
# In pipenv/cli/command.py
class PipenvGroup(DYMMixin, Group):
# ... (existing code) ...
def handle_error(self, exc_type, exc_value, traceback):
# ... (existing code) ...
elif isinstance(exc_value, exceptions.VirtualenvActivationException):
# Modify the error message here
click.secho(
'Pipenv environment for this project was previously activated in this shell.\n'
"Use the '--anyway' flag with 'pipenv shell' to reactivate.",
fg="yellow",
err=True,
)
b) Update the documentation in docs/advanced.md
:
Add a section that clearly describes the recommended workflow for activating and deactivating Pipenv environments:
## Activating and Deactivating Environments
Pipenv automatically activates the virtual environment when you use `pipenv shell`.
This creates a forked subshell where the environment is active. To deactivate the environment,
simply exit the subshell by typing `exit` or pressing Ctrl+D.
If you wish to reactivate the environment within the same shell, you can use the `--anyway` flag with `pipenv shell`:
```bash
pipenv shell --anyway
**5. Additional Steps:**
- **Testing:** The proposed changes should be thoroughly tested to ensure they address the issue without introducing regressions.
- **User Feedback:** Monitor user feedback on the updated documentation and error message to gauge their effectiveness.
This resolution prioritizes user experience by providing clear guidance and a more informative error message, while avoiding potentially disruptive modifications to external code.
Issue description
When deactivating a virtual environment created by
pipenv shell
and then trying to reactivate it usingpipenv shell
, I receive the error:However, running
unset PIPENV_ACTIVE
resolves the issue. Modifying thedeactivate
function in the virtual environment'sactivate
script to includeunset PIPENV_ACTIVE
also works.Expected result
The virtual environment should reactivate without errors when running
pipenv shell
after deactivating it.Actual result
The error message is displayed, and the virtual environment is not reactivated.
Steps to replicate
pipenv shell
.deactivate
command.pipenv shell
.System details
Additional information
To resolve the issue temporarily, adding
unset PIPENV_ACTIVE
to thedeactivate
function in the virtual environment'sactivate
script works. However, this seems to be an issue with pipenv itself.pipenv_support.md