To exit a venv, type deactivate and press Enter. It works the same in Command Prompt, PowerShell, bash, zsh and fish, and it returns the terminal to your normal Python without deleting anything.
The Python documentation puts it in one sentence: you can deactivate a virtual environment by typing deactivate in your shell. The rest of this article covers what that does, how to confirm it worked, and what to try when the command is not recognised or the prompt still shows the environment name.
How to exit a venv in each shell
There is no per-shell variation to learn. Whichever script you used to activate the environment, the exit command is the same:
deactivate
The docs add that "the exact mechanism is platform-specific and is an internal implementation detail (typically, a script or shell function will be used)". In practice that means deactivate is not a program on disk that you could run from anywhere. The activation script defines it inside your current shell session, which is why it only exists while an environment is active.
For reference, these are the activation commands from the Python docs that deactivate reverses:
| Shell | Activate | Exit |
|---|---|---|
| Windows cmd.exe | .venv\Scripts\activate.bat | deactivate |
| Windows PowerShell | .venv\Scripts\Activate.ps1 | deactivate |
| bash / zsh | source .venv/bin/activate | deactivate |
| fish | source .venv/bin/activate.fish | deactivate |
| csh / tcsh | source .venv/bin/activate.csh | deactivate |
Here .venv stands for whatever folder name you chose when you created the environment.
What deactivate does and does not do
Activation works by prepending the environment's script folder (bin on macOS and Linux, Scripts on Windows) to your PATH. That is why typing python suddenly runs the environment's interpreter. Exiting reverses that for the current terminal, so python means whatever it meant before.
It does not touch the environment itself:
- The
.venvfolder and the packages in it stay on disk. - Your project files are unchanged.
- You can activate the same environment again later with the same command.
To remove an environment, you delete its folder. The Python docs describe virtual environments as disposable, so this is safe as long as you keep a requirements.txt or similar record of what was installed. The full guide to creating and using a venv shows how to recreate one.
How to check that you have exited
The prompt prefix, such as (.venv), normally disappears when you deactivate. To be sure, ask Python directly:
python -c "import sys; print(sys.prefix != sys.base_prefix)"
The docs say it is sufficient to compare sys.prefix and sys.base_prefix to determine whether the interpreter is running from a virtual environment. Example output after a successful exit:
False
Inside an active environment the same command prints True. You can also see which interpreter the shell now finds. On Windows, where python lists the matches in PATH order. On macOS and Linux, use which python (or which python3). If the first result still points inside your .venv folder, the environment is still first on your PATH.
The docs also warn that the VIRTUAL_ENV variable "cannot be relied upon to determine whether a virtual environment is being used", because activation is optional. Use the sys.prefix comparison instead of reading that variable.
"deactivate" is not recognised
Because deactivate exists only inside a shell where an activation script ran, typing it anywhere else fails. Your shell reports the command as unknown or not found, and the exact wording depends on the shell. The usual reasons:
- You never activated in this terminal. Each terminal window or tab is its own session. If you activated in one tab and typed
deactivatein another, the second tab has nothing to undo. Nothing is wrong; you are already outside the environment there. - You activated in a different shell. Starting
cmdfrom PowerShell, or running a script that opens a subshell, creates a new session. Exit that inner shell withexitto return to the outer one, where the function is still defined. - The activation script ran as a program instead of being sourced. On macOS and Linux the docs use
source .venv/bin/activate. Running the file as./activateexecutes it in a child process, which ends before you get your prompt back, so the changes never reach your shell.
If you are unsure what state a terminal is in, run the sys.prefix check above. A result of False means you are not in a venv, whatever the prompt suggests.
The prompt still shows (venv) after deactivate
Trust the check over the prompt. If it prints False but the prefix is still on screen, you are outside the environment and only the prompt text is out of date; opening a new terminal tab clears it. If the check prints True, an environment is still active: run deactivate again and re-run the check.
Exiting a conda environment
Conda environments use a different command. If your prompt shows (base) or an environment name from conda, type:
conda deactivate
The conda docs say this removes the path name for the currently active environment from your system command. They also carry a warning about running it from the base environment: you may lose the ability to run conda in that shell, and to return to the default environment they suggest calling conda activate with no environment named. The rule of thumb is to match the tool to the environment: plain deactivate for a python -m venv folder and conda deactivate for a conda environment. If you use both, conda vs venv explains how they differ.
The conda docs also describe a setting, auto_activate, that controls whether conda activates the default environment when a shell starts. That is why a new terminal can open already inside (base), with no activation command typed by you.
Do you need to activate at all?
Often not. The docs say you do not specifically need to activate a virtual environment, because you can give the full path to the environment's interpreter, and scripts installed in the environment should be runnable without activation. Those scripts start with a shebang line that points to the environment's interpreter, so they run with it regardless of PATH. Example:
# Windows
.venv\Scripts\python.exe app.py
# macOS / Linux
.venv/bin/python app.py
Run this way, there is nothing to exit afterwards. Your shell never changed, so a stray (venv) prompt and a forgotten activation are not possible. This suits one-off commands, scheduled tasks and CI jobs.
If it still isn't working
- Close the terminal. Activation only changes that session, so a closed window is a guaranteed exit. Open a new one and run the check.
- Run the check in the terminal you are actually using. A terminal inside an editor is a separate session from the one in a standalone window, so exiting in one does not affect the other.
- Packages seem to be missing after you exit. That is expected. They live inside the environment, so outside it Python uses your system installation. Activate the environment again to get them back.
Frequently asked questions
- Does deactivate delete the virtual environment?
- No. deactivate only undoes what the activation script did to your current shell session. The environment folder and every package inside it stay on disk, and you can activate it again at any time with the same activate command you used before.
- Is there a different command to exit a venv on Windows?
- No. The Python documentation describes one command, deactivate, typed in your shell. It is the same in Command Prompt, PowerShell, Git Bash, bash, zsh and fish. The exact mechanism behind it differs by platform, but you type the same word.
- How do I exit a conda environment instead?
- Run conda deactivate. The conda documentation says it removes the active environment's path name from your system command. A venv created with python -m venv is left with plain deactivate, and a conda environment with conda deactivate.
- Do I need to deactivate before closing the terminal?
- No. Activation only changes the shell session where you ran it, so closing that terminal window ends it. Deactivating first is useful only when you want to keep working in the same terminal with your normal Python.
- How can I tell whether a venv is still active?
- Run python -c "import sys; print(sys.prefix != sys.base_prefix)". The Python documentation says this comparison is sufficient to tell whether the interpreter is running from a virtual environment. It prints True inside one and False outside.