Skip to main content

How to Activate and Deactivate Python Virtual Environments

Managing dependencies in Python is critical to avoid version conflicts between projects. Virtual environments create isolated spaces where you can install specific packages without affecting your system's global Python installation.

This guide explains how to create, activate, and deactivate Python virtual environments using the built-in venv module.

Understanding Virtual Environments

A Python virtual environment is a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.

  • Global Scope: Installing packages system-wide can break other projects that rely on different versions of the same library.
  • Isolated Scope: A virtual environment ensures that Project A can use requests==2.0 while Project B uses requests==3.0 with no conflicts.

Creating a Virtual Environment

Python 3.3+ comes with the venv module standard. To create an environment, navigate to your project directory and run the following command.

# Linux/macOS
mkdir -p ~/project/my_project
cd ~/project/my_project

# Create a virtual environment named 'venv' (or 'myenv')
python3 -m venv venv

This creates a folder named venv containing the isolated Python binaries and library folders (bin, lib, include).

note

On some Linux distributions (like Ubuntu), you may need to install the venv package first: sudo apt-get install python3-venv.

Activating the Environment

Activation is the step that modifies your shell's environment variables to point to the isolated Python interpreter instead of the system global one. The command differs slightly depending on your operating system.

Activation Commands

For Linux and macOS:

source venv/bin/activate

For Windows (Command Prompt):

venv\Scripts\activate.bat

For Windows (PowerShell):

venv\Scripts\Activate.ps1

Verifying Activation

Once activated, your command prompt usually changes to show the environment name in parentheses. You can verify the path using which (Linux/Mac) or where (Windows).

# ✅ Correct: The path points to your local project folder
which python
# Output: /home/user/project/my_project/venv/bin/python

If you are not activated:

# ⛔️ Incorrect: The path points to the system global python
which python
# Output: /usr/bin/python

Installing Packages Safely

Once activated, any package you install using pip is placed inside the virtual environment's lib directory.

# Ensure you see (venv) in your prompt
(venv) $ pip install requests

# Output:
# Collecting requests
# Installing collected packages: requests
# Successfully installed requests-2.31.0

The Risk of Not Activating

If you forget to activate the environment, you risk polluting your global system.

# ⛔️ Error/Risky: Installing without activation
# This attempts to install to the system, often requiring 'sudo' or failing
$ pip install requests
# Output: Requirement already satisfied... (or Permission denied)

# ✅ Solution: Always activate first
$ source venv/bin/activate
(venv) $ pip install requests
tip

Use pip list to see exactly what is installed in your current environment. Inside a fresh virtual environment, this list should be very short.

Deactivating the Environment

When you are finished working on your project, you should exit the virtual environment to return to your system's default Python context.

The Deactivate Command

This command is the same across all operating systems and shells (provided the environment is currently active).

(venv) $ deactivate
$

Verifying Deactivation

After running the command, the (venv) prefix will disappear from your prompt.

# Check python path again
which python
# Output: /usr/bin/python (System default)
warning

Do not try to move or rename the virtual environment folder after creating it. The scripts inside hardcode specific paths. If you move the folder, you must delete it and recreate it.

Conclusion

Using virtual environments is a best practice for Python development.

  1. Create using python3 -m venv <name>.
  2. Activate using source <name>/bin/activate (Linux/Mac) or <name>\Scripts\activate (Windows).
  3. Install packages safely with pip.
  4. Deactivate with deactivate when finished.