How to leave/exit/deactivate a Python virtualenv


Question

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

me@mymachine:~$ workon env1
(env1)me@mymachine:~$ workon env2
(env2)me@mymachine:~$ workon env1
(env1)me@mymachine:~$ 

However, how do I exit all virtual machines and workon my real machine again? Right now, the only way I have of getting back to

me@mymachine:~$ 

is to exit the shell and start a new one. That's kind of annoying. Is there a command to workon "nothing", and if so, what is it? If such a command does not exist, how would I go about creating it?

1
1375
3/27/2019 10:05:55 AM

Accepted Answer

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 procedure is a bit different: run the two-word command source deactivate since they implement deactivation using a stand-alone script.

bash-4.3$ deactivate
pyenv-virtualenv: deactivate must be sourced. Run 'source deactivate' instead of 'deactivate'
bash-4.3$ source deactivate
pyenv-virtualenv: no virtualenv has been activated.
2222
7/19/2019 7:12:56 PM

I defined an alias workoff as the opposite of workon:

alias workoff='deactivate'

Easy to remember:

[bobstein@host ~]$ workon django_project
(django_project)[bobstein@host ~]$ workoff
[bobstein@host ~]$

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon