5

When I first install packages to a virtual environment with pipenv install package and secondly try to open a subshell to operate in that environment with pipenv shell i receive the following:

"Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated. No action taken to avoid nested environments."

However, which python returns the file path of the system python, which indicated that actually the virtual environment is not activated.

ctr+D and recalling pipenv shell finally opens a subshell within the virtual environment. Can someone explain me what's going on there? Honestly, I am severely conufused.

Olmo
  • 325
  • 4
  • 13

3 Answers3

9

just cd to the folder that has your venv (.venv, Pipfile, Pipfile.lock) and run exit. it will exit the folder and now you can again cd to the same folder and use pipenv shell. it will work

  • 1
    In my cases, running `source deactivate` did not work. exiting your terminal session and re-creating one did the trick for me. – alamin May 29 '21 at 10:07
2

Exit the shell using exit and NOT deactivate. As explained here, "deactivate will leave pipenv in a confused state because you will still be in that spawned shell instance but not in an activated virtualenv."

Once you exit properly, you can navigate back to the directory and use pipenv shell

coniferous
  • 67
  • 8
1

I tried both the exit and deactivate solutions posted here yet but was still getting the message:

Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.
No action taken to avoid nested environments.

This was because on my Windows system I was using PowerShell with code in my $PROFILE to prettify the pipenv/virtualenv environment name in the console, as described here:

if ($env:PIPENV_ACTIVE -eq 1) {

    function _OLD_PROMPT { "" }
    Copy-Item -Path function:prompt -Destination function:_OLD_PROMPT


    $_PROMPT_PREFIX = (($env:VIRTUAL_ENV -split "\\")[-1] -split "-")[0]
    $pipenv_name = ((($(pip -V) -split ' ')[3]) -split '\\')[4]

    function prompt {
        Write-Host -NoNewline -ForegroundColor Green "($_PROMPT_PREFIX) " 
        _OLD_PROMPT
    
    }
}

This requires setting an environment variable PIPENV_SHELL to powershell. Once I deleted this environment variable, thereby deactivating the code, I could finally exit from the virtual environment.

I set the environment variable PIPENV_SHELL back to powershell, and am not having further problems.

dML
  • 15
  • 6