Olmo
Olmo

Reputation: 395

Using `pipenv shell` after `pipenv install` yields "Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated."

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.

Upvotes: 6

Views: 5951

Answers (3)

dML
dML

Reputation: 51

I tried both the exit and deactivate solutions posted here 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.

Upvotes: 1

coniferous
coniferous

Reputation: 161

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

Upvotes: 4

Frozen Strawberries
Frozen Strawberries

Reputation: 141

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

Upvotes: 10

Related Questions