Virtual Environment (venv)

A virtual environment in Python is an isolated workspace where you can install packages and run projects without affecting the system-wide Python installation. Each project can have its own dependencies, versions, and configuration. This avoids conflicts between projects and ensures long-term stability.

Virtual environments are essential for professional development because real applications often require specific versions of libraries. Using venv makes a project self-contained, reproducible, and easier to maintain.

1. What Is a Virtual Environment

A virtual environment is a dedicated folder that contains:

  • A separate Python interpreter
  • Its own site-packages directory
  • Project-specific installed packages

This isolation ensures that:

  • Different projects do not conflict
  • Upgrading one package does not break other projects
  • Developers work with consistent versions across machines
  • Deployments become predictable

Without virtual environments, global installations can quickly become unmanageable, especially when multiple projects require different versions of the same library.

2. Why Virtual Environments Are Needed

Real-world Python applications commonly depend on libraries like Flask, Django, NumPy, Pandas, or TensorFlow. These libraries release frequent updates. One project may rely on an older version, while another may require the latest.

Using venv solves these issues by allowing each project to maintain its own set of packages.

Benefits:

  1. Isolation – each project has its own packages
  2. No system modification – system Python remains clean
  3. Version control – exact package versions can be recorded in requirements.txt
  4. Reproducibility – other developers can recreate the same environment
  5. Safe experimentation – test new libraries without affecting system Python

3. Creating a Virtual Environment

Python provides the built-in venv module to create a virtual environment.

Command

python -m venv myenv

This creates a directory named myenv containing:

  • Scripts/ or bin/ folder
  • A private Python executable
  • Local package installation path

No output appears if the command succeeds.

4. Activating the Virtual Environment

Activation switches your terminal to use the environment’s Python instead of the system Python.

On Windows

myenv\Scripts\activate

On macOS and Linux

source myenv/bin/activate

Output:

Your terminal prompt changes to:

(myenv) C:\Users\User>

This indicates the environment is active.

All pip install commands now install packages into myenv, not globally.

5. Deactivating the Environment

To exit the virtual environment:

deactivate

Output:

The environment name disappears from the terminal prompt.

6. Installing Packages Inside a Virtual Environment

Once activated, you can install packages normally using pip.

pip install flask

Checking installation:

pip list

Output:

Flask  2.3.0
pip    23.2
setuptools 65.0

All packages exist inside the environment’s folder.

7. Requirements File (requirements.txt)

A virtual environment works best when you save a list of packages required for your project. This allows other developers or servers to recreate the same environment.

Creating a requirements file

pip freeze > requirements.txt

Installing from requirements.txt

pip install -r requirements.txt

This ensures complete environment replication.

8. Removing a Virtual Environment

Virtual environments are self-contained. To delete one, simply remove the folder.

rm -r myenv

or on Windows:

rmdir /s /q myenv

No special deactivation or cleanup is required.

9. Using VS Code with Virtual Environments

Most developers use VS Code with venv.

Steps:

  • Open your project folder
  • Create or activate your virtual environment
  • VS Code automatically detects it
  • At the bottom-right, VS Code will show the selected interpreter
  • You can manually choose it:
    Command Palette → Python: Select Interpreter → choose myenv

VS Code will now use the virtual environment’s Python and packages.