Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Leaving and Switching Between Virtual Environments

00:00 Another important aspect that I would like to cover is how to leave an active virtual environment, and then also how to switch between different virtual environments.

00:11 So again, I am going to jump to the terminal for this. Alright, so I am back in my example project, inside an active virtual environment. Now, how do you get back to the global environment if you are inside an active virtual env?

00:25 And the answer for that is pretty easy, you just run the deactivate command. So the deactivate command is available every time we’re working in an active Python virtual environment.

00:35 The global environment does not support this deactivate command. Now you can see here that when I ran the deactivate command, it actually removed this little marker here, from my shell prompt, so now I can tell that I am back in the global environment.

00:51 One more thing I wanted to mention is how do you actually switch between different virtual environments if you are working with multiple projects. So right now I am still in this virtual environment for my test project here, and what I am going to do here is I am going to leave that environment and then I am going to go to a different project folder that also has a virtual environment.

01:13 And here, I would just use the same activate command to switch into that virtual environment and then start working from here. And it’s a good idea to deactivate or leave an active virtual environment before you go and activate or enter a new environment.

aravind on Sept. 17, 2019

is it possible to customize the (venv) text shown? Lets say you are working on 2 or more virtual envs at the same time in different terminals, need a way to visually distinguish which venv you are currently in.

Dan Bader RP Team on Sept. 17, 2019

Great question! The virtualenv prefix in your shell prompt is determined by the folder name of the virtualenv. So in my example it’s (venv) because the directory is called venv/.

If you create a virtual environment with a different folder name, the prompt text will be different as well to reflect that change. So for example you could create a second virtual env like this:

$ python3 -m venv my_virtualenv
$ . /my_virtualenv/bin/activate
(my_virtualenv) $

aravind on Sept. 17, 2019

Thanks Dan, clear.

Dan Bader RP Team on Sept. 17, 2019

Ok great, you’re welcome!

Eelco on Nov. 11, 2020

Only then your nice aliases won’t work any more. Or one needs to create a naming scheme like venv_myprojectname and build a bit more logic in the aliases you show on one of the next slides.

Dan Bader RP Team on Nov. 12, 2020

@Eelco: Yep you’re absolutely right, that’s a downside of this approach and why I’m a big fan of the “one venv per project, under a standard naming scheme” system.

DoubleA on Jan. 16, 2021

Hey Dan, what a clear explanation! Really appreciated learning this non-trivial stuff. Perhaps one side note for those on Windows machines: when playing with vevns I’ve discovered that one can deactivate an active venv being anywhere in the OS file system tree, even in the route directory. However, for activating a specific vevn one must be in the Script directory of the target venv.

Become a Member to join the conversation.