Question

How to Exit a Python Virtual Environment and Return to the System Python

pythonvirtualenvexitvirtualenvwrapper

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 python executable is used first
  • the environment's pip is 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 from env1.
  • When you activate env2, you now see Python tools from env2.
  • 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.

Take Quiz

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

$ which python
$ which pip

Typical results after deactivation might be:

/usr/bin/python
/usr/bin/pip

or on some systems:

/usr/local/bin/python3
/usr/local/bin/pip3

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:

  1. workon env1

    • virtualenvwrapper activates the env1 environment.
    • It updates your shell PATH so env1/bin comes first.
    • Your prompt changes to show (env1).
  2. which python

    • The shell checks which python command will run.
    • Because env1/bin is first in PATH, it returns the Python inside that environment.
  3. deactivate

    • The shell runs the deactivate function created during activation.
    • It restores the old PATH.
    • It removes the virtual environment prompt.
    • It clears environment variables such as VIRTUAL_ENV.
  4. which python

    • Now the shell finds the system Python again because the virtual environment path is no longer first.

A useful command for checking the active environment is:

echo $VIRTUAL_ENV
  • If an environment is active, this usually prints its path.
  • If nothing is active, it prints nothing.

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.
Take Quiz

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 deactivate before switching context or using system-level Python tools.
  • Check the current interpreter

    • Commands like which python, python -V, and echo $VIRTUAL_ENV help avoid confusion.
  • 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

A quick check prevents installing into the wrong place:

which pip

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:

echo $VIRTUAL_ENV

4. Confusing deactivation with deletion

deactivate does not remove the environment. It only stops using it in the current shell.

Deleting an environment is a separate action.

5. Forgetting that each shell tab is separate

If you activate an environment in one terminal tab, other tabs are unaffected. Deactivation also applies only to the current shell session.

6. Installing packages after forgetting which environment is active

Broken workflow:

(env1) $ pip install requests

If you thought you were using the system Python, this installs into env1 instead.

Safer workflow:

$ which pip
$ which python

Comparisons

ActionCommandWhat it does
Activate a virtual environment manuallysource venv/bin/activateStarts using that environment in the current shell
Activate with virtualenvwrapperworkon env1Switches into a named environment
Deactivate current environmentdeactivateReturns to the system shell environment
Close the shellexitEnds the terminal session entirely

deactivate vs exit

CommandEffect
deactivateLeaves the virtual environment but keeps the shell open
exitCloses the shell itself

workon env2 vs deactivate

CommandBest used when
workon env2You want to move directly from one virtual environment to another
deactivateYou want to use no virtual environment and return to system Python

Virtual environment tools

ToolTypical command to enterTypical command to leave
virtualenvsource venv/bin/activatedeactivate
virtualenvwrapperworkon myenvdeactivate
venvsource .venv/bin/activatedeactivate
Take Quiz

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

  • deactivate leaves the current virtual environment.
  • deactivate does not delete the environment.
  • exit closes the shell, which is usually not what you want.
  • workon switches into an environment, but deactivate switches 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_ENV prints a path
  • which python points 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:

workon another_env

What if deactivate says command not found?

That usually means no virtual environment is currently active in that shell.

Is this the same for Python venv?

Yes. The deactivation command is typically still:

deactivate
Take Quiz

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

  1. Create a new virtual environment.
  2. Activate the environment in your shell.
  3. Verify that python points to the environment version.
  4. Deactivate the environment.
  5. Verify that python points back to the system version.
Take Quiz

Keep learning

Related questions

View more