83

How can I deactivate my pipenv environment?

With other tools I've been able to do something like source deactivate, but that has no affect here.

Create an environment:

pipenv --three

Activate the environment:

source $(pipenv --venv)/bin/activate

But how to deactivate?

tim_xyz
  • 11,573
  • 17
  • 52
  • 97
  • Just a tip, when using pipenv as your package and environment manager, the tool already provides an easier command to activate the virtual environment, which is `pipenv shell` as stated in the [documentation](https://docs.pipenv.org/en/latest/basics/#example-pipenv-workflow), so no `source $(pipenv --venv)/bin/activate` required. – dandev486 May 08 '19 at 11:19
  • Type `exit` and you'll be in your bash. – Sadra Nov 09 '20 at 12:50
  • 4
    The [current accepted answer](https://stackoverflow.com/a/49944909) is actually incorrect and even messes things up, as explained [below](https://stackoverflow.com/a/51075851). Could you please accept one of the correct answers? – djvg Dec 14 '20 at 11:26

8 Answers8

91

To elaborate on Williams' answer a bit more, the expected workflow is to enter the virtualenv using pipenv shell. When you activate the virtualenv that way, the console output now indicates to use exit:

Spawning environment shell (/bin/zsh). Use 'exit' to leave.

Trying to exit the virtualenv with deactivate will leave pipenv in a confused state because you will still be in that spawned shell instance but not in an activated virtualenv.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ButtaKnife
  • 911
  • 1
  • 6
  • 2
57

Using the shell command exit also works.

This worked for me when using deactivate still left me with the error:

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

After using exit I could successfully switch pipenv instances. This could well be a bug and I should probably report it.

Williams
  • 4,044
  • 1
  • 37
  • 53
  • 8
    From my understanding after reading through the issues on GitHub around this topic it isn't a bug. This (https://github.com/pypa/pipenv/issues/84) particular ticket helped enlighten me... in summary, you can open a new shell and proceed as usual, or type `exit` and proceed as usual. – Jarvis Jun 28 '18 at 10:33
35

UPDATE: See other answers below. As it has been explained, this works for virtualenv, but pipenv works differently.

Just type deactivate on the command line. See the guide here

ohduran
  • 787
  • 9
  • 15
  • 40
    The command deactivate would work for virtualenv but the tool used here is pipenv which works differently and the deactivate command will not work. One has to type exit in shell to close the virtual environment spawned by pipenv command. The answer is misleading – Saransh Singh Dec 18 '18 at 15:31
  • Link to guide is no longer correct – moojen Nov 19 '21 at 13:03
17

Just type exit, and it will take you out of your shell.

And if you use deactivate, you will probably get the below error when you try to enter the shell again.

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

So, it is prefer to use "exit" for quick access.

Jigyasu Dhingra
  • 171
  • 1
  • 4
  • 1
    Quick note, if you have this error you can simply type 'exit' in the affected directory and it should successfully deactivate the pipenv shell. – Olivier Sep 12 '22 at 18:15
11

Deactivate from pipenv virtual environment, you can simply run the following command.

deactivate
Mahfuz Khandaker
  • 454
  • 5
  • 12
7

Please check this topic for answer from contributor perspective:

https://github.com/pypa/pipenv/issues/84#issuecomment-275056943

Spoiler :

pipenv starts a new shell session with the virtualenv pathing instead of changing the pathing in the current shell session. That is why deactivate does not work. you need to exit the shell session. the exit command or CTRL-d will do the trick.

0

First "deactivate" and then "exit" you will get out of the virtual env. As of now this worked for me.

  • 8
    The command `exit` already does the entire job, since `pipenv shell` spawns a new shell process as stated in the end of [this documentation block](https://docs.pipenv.org/en/latest/basics/#example-pipenv-workflow), no need to `deactivate` first. – dandev486 May 08 '19 at 11:26
  • 1
    It looks like @dandev486 's link is dead and the [new page](https://docs.pipenv.org/basics/#about-shell-configuration) does not really talk about how to exit. The [old docs](https://web.archive.org/web/20190717063720/https://docs.pipenv.org/en/latest/basics/#example-pipenv-workflow), FWIW, said "This will spawn a new shell subprocess, which can be deactivated by using exit.". Personally I think `exit` is still probably the right way to leave an env. – jrh Aug 26 '21 at 15:02
0

If you are using pipenv shell to activate virtual environment, and exit or deactivate doesn't work. Right click and kill terminal. That works for me

user16217248
  • 3,119
  • 19
  • 19
  • 37