Categories
exit python virtualenv virtualenvwrapper

How to leave/exit/deactivate a Python virtualenv

2211

I’m using virtualenv and the virtualenvwrapper. I can switch between virtualenv’s just fine using the workon command.

[email protected]:~$ workon env1
(env1)[email protected]:~$ workon env2
(env2)[email protected]:~$ workon env1
(env1)[email protected]:~$ 

How do I exit all virtual environments and work on my system environment again? Right now, the only way I have of getting back to [email protected]:~$ is to exit the shell and start a new one. That’s kind of annoying. Is there a command to work on “nothing”, and if so, what is it? If such a command does not exist, how would I go about creating it?

3

  • 7

    There is a command to workon “nothing” – it displays all your available virtual environments, which is pretty nifty. Just type “workon” with no arguments and hit enter. The command to leave is “deactivate”, as answered below.

    – Dannid

    Oct 7, 2014 at 20:47

  • Does closing the shell deactivate all the environments used the shell session. Just wanted to get an idea if it modifies the path env variable of the windows and just leaves it like that once the shell is closed ?

    Apr 26, 2021 at 7:54

  • 1

    @MukeshMahadev Yes, activation is only for the current shell.

    – Apreche

    Apr 26, 2021 at 13:50

3546

Usually, activating a virtualenv gives you a shell function named:

$ deactivate

which puts things back to normal.

I have just looked specifically again at the code for virtualenvwrapper, and, yes, it too supports deactivate as the way to escape from all virtualenvs.

If you are trying to leave an Anaconda environment, the command depends upon your version of conda. Recent versions (like 4.6) install a conda function directly in your shell, in which case you run:

conda deactivate

Older conda versions instead implement deactivation using a stand-alone script:

source deactivate

7

  • 178

    The “deactivate” command is not a binary, nor a script that you “source”; it is a shell alias that gets defined dynamically in your current shell by the “activate” script.

    Feb 6, 2013 at 22:28

  • 7

    @Apreche In the meantime (almost four years later) this appears to have been added to the documentation.

    – gertvdijk

    Mar 14, 2013 at 14:26

  • 8

    Would be much more intuitive if it were called “workoff” or “unworkon”. Or if “workon” were called “activate”. Thank goodness for alias.

    – kkurian

    Jun 18, 2013 at 17:54

  • 5

    @kkurian — you should suggest that on the issue tracker for virtualenvwrapper and maybe Doug Hellmann would consider it! Note, for those who might read these comments later, that workon is NOT a native virtualenv command (which is what the original question is about) but a virtualenvwrapper command!

    Jun 29, 2013 at 23:42

  • 26

    Guess what the actual virtualenv command inside of “workon” is called? …(spoiler warning)… …(spoiler warning)… …(spoiler warning)… …(spoiler warning)… activate!

    Mar 20, 2014 at 5:18


94

Use:

$ deactivate 

If this doesn’t work, try

$ source deactivate

Anyone who knows how Bash source works will think that’s odd, but some wrappers/workflows around virtualenv implement it as a complement/counterpart to source activate. Your mileage may vary.

8

  • 14

    deactivate is a function that gets created when you source the activate file. Your suggestion to do source deactivate doesn’t make sense at all, as there is no file named deactivate

    – Anthon

    Apr 12, 2015 at 8:14


  • 11

    This doesn’t deserve the downvotes. See edit of selected response: source deactivate is for the anaconda environment.

    Nov 13, 2015 at 19:52

  • 5

    It “deserves” downvotes for not meeting the SO answer quality standards. It’s more of a comment than an answer. But, because of the 79 reputation of the poster, we should be nice and give good feedback.

    Mar 17, 2017 at 15:22

  • @Abdul I have demonstrated how you can improve your answer quality in Revision 2 at stackoverflow.com/posts/29586756/revisions

    Mar 17, 2017 at 15:30

  • this is very unhelpful if you don’t have a deactivate command in your shell. I don’t really understand why this would help the problem. There is no deactivate script in the virtual env.

    – bgenchel

    Feb 28, 2018 at 6:26

61

I defined an alias, workoff, as the opposite of workon:

alias workoff="deactivate"

It is easy to remember:

[[email protected] ~]$ workon django_project
(django_project)[[email protected] ~]$ workoff
[[email protected] ~]$

4

  • 1

    In which file? .bashrc?

    – seyed

    Jun 8, 2015 at 17:59

  • @seyed yes, see this answer for an example of alias in ~/.bashrc

    – Bob Stein

    Jun 8, 2015 at 19:37

  • 25

    I like this alias. Reminds me of the Karate Kid (waxon; waxoff)

    Oct 2, 2016 at 1:59

  • @C0deH4cker: I signed into SO and came back to this question just to +1 your comment :p

    Nov 15, 2019 at 22:24