Run the deactivate command directly in your terminal to exit an active Python virtual environment and restore your shell’s default system PATH. This works identically across Windows, macOS, and Linux for standard virtual environments (venv and virtualenv).

Basic Example

# 1. Confirm you are inside an active virtual environment (shown by the prefix)
(venv) $ which python
/path/to/project/venv/bin/python

# 2. Run deactivate to exit the environment
(venv) $ deactivate

# 3. Verify your prompt and Python interpreter have returned to the global system
$ which python
/usr/bin/python3

How It Works Under the Hood

  • Environment restoration: Activating a virtual environment temporarily prepends its bin/ (or Scripts\ on Windows) directory to your shell’s $PATH and saves the original state in _OLD_VIRTUAL_PATH. - Reverting changes: Running deactivate executes a shell function defined inside the activate script that restores _OLD_VIRTUAL_PATH, resets the terminal prompt ($PS1), and unsets the VIRTUAL_ENV environment variable.
  • No path required: Because deactivate is loaded into your active shell’s memory during activation, you can run it from any working directory—you do not need to navigate back to the project folder.

Environment-Specific Variations

Tool / Environment Exit Command Notes
venv / virtualenv deactivate Standard across Bash, Zsh, PowerShell, and Windows Command Prompt.
Conda / Miniconda conda deactivate Older Conda versions used source deactivate, which is now deprecated.
Poetry (poetry shell) exit poetry shell spawns a nested subshell; exit closes it cleanly.
Pipenv (pipenv shell) exit Similar to Poetry, exit terminates the spawned subshell session.
VS Code / IDE Terminal deactivate Closing the integrated terminal tab also ends the session automatically.

Frequently Asked Questions

What happens if I just close the terminal window instead of running deactivate?
Nothing breaks. Virtual environment changes only exist in the memory of that specific terminal session, so closing the terminal terminates the environment cleanly without affecting your project files or installed packages.

Why does deactivate say “command not found”?
This occurs if no virtual environment is currently active in that terminal tab, or if you entered a nested subshell (such as poetry shell or pipenv shell) where you must type exit instead.