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/(orScripts\on Windows) directory to your shell’sand saves the original state in_OLD_VIRTUAL_PATH. - Reverting changes: Runningdeactivateexecutes a shell function defined inside theactivatescript that restores_OLD_VIRTUAL_PATH, resets the terminal prompt (), and unsets theVIRTUAL_ENVenvironment variable. - No path required: Because
deactivateis 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.
