Question
How to Exit a Python Virtual Environment and Return to the System Python
Question
I'm using virtualenv and virtualenvwrapper, and I can switch between environments with the workon command:
$ workon env1
(env1) me@mymachine:~$ workon env2
(env2) me@mymachine:~$ workon env1
(env1) me@mymachine:~$
How can I leave all virtual environments and return to the normal system environment? At the moment, the only way I can get back to a plain shell like this is by closing the shell and opening a new one:
me@mymachine:~$
Is there a command to deactivate the current environment and work on no virtual environment at all? If so, what is it? If not, how could I create that behavior?
Short Answer
By the end of this page, you will understand how Python virtual environments are activated and deactivated, how to return to your system Python, and how this works in both virtualenv and virtualenvwrapper. You will also see the common command used in practice, why it works, and mistakes beginners often make when switching environments.
Concept
A Python virtual environment is an isolated Python setup for a project. It gives you a separate place for installed packages, so one project can use different dependencies from another project without conflicts.
When you activate a virtual environment, your shell session is temporarily changed so that:
- the environment's
pythonexecutable is used first - the environment's
pipis used first - your shell prompt often shows the environment name
- some environment variables, such as
PATH, are adjusted
When you want to stop using that environment, you deactivate it. Deactivation restores your shell so it uses the system Python again.
This matters because real projects often depend on different library versions. Virtual environments let you keep those dependencies separate, but you also need a reliable way to leave one environment and return to your normal shell.
In virtualenv and virtualenvwrapper, the usual way to leave the active environment is:
deactivate
If you are using virtualenvwrapper, workon is used to switch into environments, while deactivate is used to leave the current one.
Mental Model
Think of a virtual environment like wearing a project-specific pair of glasses.
- When you activate
env1, you see Python tools fromenv1. - When you activate
env2, you now see Python tools fromenv2. - When you run
deactivate, you take the glasses off and go back to normal vision: the system Python.
You are not deleting anything and you are not uninstalling the environment. You are only changing which Python tools your current shell session is using.
Syntax and Examples
The core command is:
deactivate
Example with virtualenv
$ source venv/bin/activate
(venv) $ python --version
(venv) $ deactivate
$ python --version
After deactivate, the prompt no longer shows (venv), and your shell uses the normal Python from your system PATH.
Example with virtualenvwrapper
$ workon env1
(env1) $ which python
/home/me/.virtualenvs/env1/bin/python
(env1) $ deactivate
$ which python
/usr/bin/python
Switching directly between environments
virtualenvwrapper also lets you switch from one environment to another:
$ workon env1
(env1) $ workon env2
(env2) $
But if you want to use no virtual environment at all, use:
deactivate
How to verify you are back to the system environment
Step by Step Execution
Consider this shell session:
$ workon env1
(env1) $ which python
/home/me/.virtualenvs/env1/bin/python
(env1) $ deactivate
$ which python
/usr/bin/python
Here is what happens step by step:
-
workon env1virtualenvwrapperactivates theenv1environment.- It updates your shell
PATHsoenv1/bincomes first. - Your prompt changes to show
(env1).
-
which python- The shell checks which
pythoncommand will run. - Because
env1/binis first inPATH, it returns the Python inside that environment.
- The shell checks which
-
deactivate- The shell runs the deactivate function created during activation.
- It restores the old
PATH. - It removes the virtual environment prompt.
Real World Use Cases
Virtual environment deactivation is useful in many everyday Python workflows:
-
Working on multiple projects
- Activate one project environment, do some work, then deactivate before moving to a different task.
-
Running system Python tools
- Some scripts are meant to use your system Python, not a project-specific one.
-
Debugging dependency issues
- If a command behaves strangely, deactivating helps you check whether the virtual environment is causing the issue.
-
Installing global tools carefully
- Before installing something globally, you may want to make sure you are not still inside a virtual environment.
-
Teaching and tutorials
- Beginners often need to confirm whether a package was installed globally or only inside a virtual environment.
Real Codebase Usage
In real projects, developers usually treat virtual environments as part of the project workflow.
Common patterns
-
Activate when entering a project
- Developers activate the project's environment before running tests, scripts, or servers.
-
Deactivate when done
- They run
deactivatebefore switching context or using system-level Python tools.
- They run
-
Check the current interpreter
- Commands like
which python,python -V, andecho $VIRTUAL_ENVhelp avoid confusion.
- Commands like
-
Use shell automation
- Some teams use tools that auto-activate environments when entering a directory.
Guard-style checks in scripts
Many codebases avoid depending on manual activation by calling the environment's Python directly:
./venv/bin/python script.py
This is similar to a guard clause in programming: it avoids ambiguity by being explicit.
Validation before installing packages
Developers often verify the target environment before running:
pip install package-name
Common Mistakes
1. Trying to use exit instead of deactivate
Broken approach:
$ exit
This closes the shell entirely instead of just leaving the virtual environment.
Use this instead:
$ deactivate
2. Thinking workon with no name will always deactivate
Some users assume this will return to the system Python:
$ workon
In virtualenvwrapper, workon without arguments typically lists environments rather than deactivating the current one.
Use:
$ deactivate
3. Running deactivate in a shell where no environment is active
If no environment is active, deactivate may not exist as a command in that shell session.
Check first:
Comparisons
| Action | Command | What it does |
|---|---|---|
| Activate a virtual environment manually | source venv/bin/activate | Starts using that environment in the current shell |
| Activate with virtualenvwrapper | workon env1 | Switches into a named environment |
| Deactivate current environment | deactivate | Returns to the system shell environment |
| Close the shell | exit | Ends the terminal session entirely |
deactivate vs exit
| Command | Effect |
|---|
Cheat Sheet
# Activate a virtual environment
source venv/bin/activate
# Activate with virtualenvwrapper
workon myenv
# Leave the current virtual environment
deactivate
# Check which Python is active
which python
# Check which pip is active
which pip
# Check whether a virtual environment is active
echo $VIRTUAL_ENV
Rules to remember
deactivateleaves the current virtual environment.deactivatedoes not delete the environment.exitcloses the shell, which is usually not what you want.workonswitches into an environment, butdeactivateswitches back to none.- Activation and deactivation only affect the current shell session.
Quick signs an environment is active
- Your prompt includes something like
(env1) echo $VIRTUAL_ENVprints a pathwhich pythonpoints inside a virtual environment directory
FAQ
How do I exit a Python virtual environment?
Use:
deactivate
This returns your shell to the system Python.
How do I deactivate a virtualenvwrapper environment?
Also use:
deactivate
Even if you entered it with workon, you leave it with deactivate.
Does deactivate delete my virtual environment?
No. It only stops using the environment in the current shell.
Why does exit seem to work?
Because closing the shell also ends the activated session. But it is heavier than needed and closes your terminal session completely.
How can I tell if I am still inside a virtual environment?
Check:
echo $VIRTUAL_ENV
which python
Can I switch directly from one virtual environment to another?
Yes. With virtualenvwrapper, you can run:
Mini Project
Description
Create a small terminal workflow exercise that helps you practice activating a Python virtual environment, checking which Python interpreter is active, and then deactivating it correctly. This mirrors real development work where you need to confirm whether commands run inside a project environment or against the system Python.
Goal
Practice entering and leaving a Python virtual environment and verifying which interpreter your shell is using.
Requirements
- Create a new virtual environment.
- Activate the environment in your shell.
- Verify that
pythonpoints to the environment version. - Deactivate the environment.
- Verify that
pythonpoints back to the system version.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.
Convert Bytes to String in Python 3
Learn how to convert bytes to str in Python 3 using decode(), text mode, and proper encodings with practical examples.