Open In App

How to leave/exit/deactivate a Python virtualenv

Last Updated : 14 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A virtual environment is like a separate workspace for your Python projects. It’s a self-contained folder that has its own Python installation and any libraries or packages your project needs, completely isolated from other projects. Its key benefits include:

  • Keeping dependencies organized and project-specific
  • Avoiding version conflicts between projects
  • Maintaining a clean global Python environment

Why use a virtual environment?

Think of a virtual environment as a safety bubble for each of your Python projects. Let’s say you're working on two different projects:

  • Project A needs Python 3.8 and Flask 1.1
  • Project B needs Python 3.10 and Flask 2.3

If you install everything globally on your main system , one project might break the other due to version mismatches. That's where virtual environments come in. They allow you to:

  • Create isolated spaces for each project
  • Use different versions of Python and libraries side by side
  • Avoid messy conflicts between dependencies
  • Keep your global Python setup clean and untouched

How to create a Python Virtual environment?

To manage dependencies and isolate project environments, it’s best practice to create a virtual environment. You can easily set one up using the steps below.

Step 1: Use pip to install the virtualenv package:

pip3 install virtualenv
Install a Virtual Environment Python
Installing virtualenv

Now check your installation

virtualenv --version

Step 2: Create a virtual environment. After running the below command, a directory named virtualenv_name will be created. This is the directory that contains all the necessary executables to use the packages that a Python project would need. 

virtualenv virtualenv_name

Step 3: Create a new virtual environment

virtualenv -p /usr/bin/python3 venv

where venv is the name of the virtual environment you can change it accordingly

Creating a Virtual Environment Python
Create new venv

Step 4: Activate our newly created virtual environment

source venv/bin/activate
Activate a Virtual Environment Python
activated venv

If everything works well then the name of your virtual environment will appear on the left side of the terminal as shown in the above image which means that the virtual environment is currently active.

How to Deactivate or Exit the Virtual Environment

Once you're done working in the virtual environment, it’s good practice to deactivate it to avoid unintentional use by simply executing the below command.

deactivate

Deactivate a Virtual Environment Python
Deactivated venv

As you can see in the above image the name of our virtual environment disappeared.


Next Article
Practice Tags :

Similar Reads