How to leave a virtual environment in Python? Code Example

Total
0
Shares

In order to leave a virtual environment you can close the shell terminal or command prompt. That will end the session.

But Python also provides deactivate command which is used to terminate or leave a virtual environment and switch back to the system installed version of Python.

Suppose you are into a virtual environment. We can check it using echo $VIRTUAL_ENV

(env1)$ echo $VIRTUAL_ENV
/Users/akash/virtualenvwrapper/tmp/env1   # 👈 Output

How to create virtual environment?

Use mkvirtualenv env1 to create Virtual environment in Python. Also you will be switched to this environment.

Now to return back to system environment, we can use deactivate

(env1)$ deactivate

Now run echo $VIRTUAL_ENV again to check if we are still into a virtual environment. If the output is empty then we are not in any virtual environment.

$ echo $VIRTUAL_ENV

$

If you are using anaconda, then use either of these commands –

(env1)$ conda deactivate

# ☝ī¸ OR 👇

(env1)$ source deactivate
deactivate virtual environment in conda using conda deactivate