When you install a software package globally, it's shared across all your Python projects. This can cause conflicts, especially if different projects need different versions of the same package.
To remedy this, you can isolate each project and manage its dependencies separately in a Python virtual environment. After you finish working in one environment, it's good practice to deactivate it before switching to another.
Learn how to deactivate and delete a Python virtual environment with a few simple commands.

Prerequisites
- Python installed (see our guides on installing Python on Ubuntu, Windows, and macOS).
- An existing virtual environment.
- Access to a terminal or command line.
Why Deactivate Python Virtual Environment?
A Python virtual environment modifies your shell's environment variables, so you can work in an isolated space separate from the system's main Python.
Note: To avoid compatibility issues, check your Python version and, if needed, update Python before creating virtual environments or installing packages.
If you activate a new environment without turning off the one you are currently working in, the new environment usually takes precedence and should work without issues. However, there are a few things to keep in mind:
- Even though the new environment overrides the old one,
PATHentries from different environments may stack in unexpected ways. - With multiple environments, the shell can become harder to follow, and you may lose track of which environment is active.
To keep things simple and avoid carrying over settings from a previous environment, it's best to deactivate the current environment before switching to a new one.
How to Deactivate Python Virtual Environment?
To deactivate a Python virtual environment created with the venv module, enter:
deactivate
The command works on Linux, macOS, and Windows.

If you are working inside a Pipenv or Poetry shell, type:
exit
When you deactivate the environment, it still exists, and you can reactivate it at any time. However, in some cases, you may also want to delete it, like when you are done with a project, want to recreate the environment from scratch, or need to free up disk space.
Note: Virtual environments can take up a lot of disk space over time. If you are running low on storage, learn how to check available disk space on your Linux system.
How to Delete Python Virtual Environment
There are several ways to delete a virtual environment, depending on the tool you are using.
Using venv or virtualenv
If you created a virtual environment using venv or virtualenv, it exists as a directory inside your project or filesystem. In this example, the venv_experiment environment is in the mypy_project directory.

Once you have deactivated venv_experiment, you can delete it by removing its directory. On Linux or macOS, use the rm command:
rm -rf venv_experiment
The command does not produce output. You can use the ls command to confirm the virtual environment is no longer in the mypy_project directory.
If you are using Windows, open the Command Prompt and enter the following command to remove the Python virtual environment:
rmdir /s /q venv_experiment
If you are using PowerShell, run this command instead:
Remove-Item -Recurse -Force venv_experiment
This will permanently delete the virtual environment and all its contents.
Using Pipenv
The Pipenv tool automatically sets up a virtual environment for your project. You can use it to manage software packages and virtual environments from a single workflow.
Navigate to your project directory, and use the following Pipenv command to remove the virtual environment:
pipenv remove
Pipenv confirms the virtual environment was removed.

This deletes the virtual environment associated with the current project.
Using Poetry
Poetry is an all-in-one tool that helps developers manage project dependencies, publish packages, and set up virtual environments in a consistent and reliable way.
To remove a Poetry-managed environment, use the following command and provide the environment name:
poetry env remove [environment-name]
To find the exact environment name, list the existing environments:
poetry env list

In this example, the environment name is mypy-project-QuuvEIpA-py3.12. To remove it, type:
poetry env remove mypy-project-QuuvEIpA-py3.12

The system confirms the virtual environment was deleted.
Conclusion
This guide showed how to deactivate and remove a Python virtual environment using different tools across major operating systems.
If you get a ModuleNotFoundError while using virtual environments, read our guide to learn its causes and how to fix it.



