Master Python Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Virtual Environments in Python

Learn how to create an isolated Python environment for a project and manage its packages separately.

What is a Virtual Environment?

A virtual environment is an isolated environment created for a Python project.

It allows a project to have its own Python packages and package versions without affecting other projects.

Simple Definition: A virtual environment is a separate Python environment used to keep project dependencies isolated.

Why Do We Need Virtual Environments?

Different projects may require different versions of the same package.

For example, one project may need one version of a library while another project needs a different version.

A virtual environment keeps these dependencies separate.

Isolation

Each project can have its own packages.

Dependency Control

Manage package versions separately for each project.

Safer Projects

Installing one project's packages does not have to change another project.

Virtual Environment Diagram

A project can have its own isolated environment containing its dependencies.

Python Projects

Multiple projects may need different package versions.

↓
Project A

Virtual Environment A
Own packages and versions

Project B

Virtual Environment B
Own packages and versions

↓
Isolated Dependencies

Changes in one project do not have to affect the other project's environment.

Creating a Virtual Environment

Python provides the venv module for creating virtual environments.

Open the terminal inside your project folder and run:

python -m venv venv

Here:

  • python runs Python.
  • -m venv uses Python's virtual environment module.
  • venv is the name of the environment folder in this example.

Activate the Environment on Windows

In Windows Command Prompt:

venv\Scripts\activate

In Windows PowerShell:

.\venv\Scripts\Activate.ps1

After activation, the environment name is usually shown at the beginning of the terminal prompt.

Activate the Environment on macOS / Linux

source venv/bin/activate

The command activates the venv environment.

Install Packages Inside the Environment

After activating the virtual environment, install the required package.

pip install requests

The package is installed into the active environment.

Other projects using different environments can maintain their own package setup.

Check the Environment

You can check the installed packages using:

pip list

You can also check which Python executable is being used.

Windows

where python

macOS / Linux

which python

Using requirements.txt

A project can keep a list of its dependencies in a requirements.txt file.

Example:

requests==2.x.x
flask==3.x.x

Install the required packages using:

pip install -r requirements.txt

This makes it easier to set up the same project dependencies in another environment.

Deactivate a Virtual Environment

When you finish working with the environment, you can deactivate it using:

deactivate

After deactivation, the terminal returns to the normal Python environment.

Deleting a Virtual Environment

A virtual environment is mainly a folder in your project.

After deactivating it, the environment folder can be removed when it is no longer required.

For example, if the environment folder is named venv, remove that folder from the project.

Common Virtual Environment Commands

Command Purpose
python -m venv venv Create a virtual environment.
venv\Scripts\activate Activate on Windows Command Prompt.
.\venv\Scripts\Activate.ps1 Activate in Windows PowerShell.
source venv/bin/activate Activate on macOS / Linux.
deactivate Deactivate the environment.

Project Structure

After creating a virtual environment, a simple project may look like this:

myproject/
|
|-- venv/
|
|-- main.py
|
|-- requirements.txt

The venv folder contains the virtual environment, while the other files belong to the project.

Virtual Environment vs Global Environment

Virtual Environment Global Environment
Project-specific environment. General Python environment.
Dependencies can be isolated. Packages are installed for the broader Python environment.
Different projects can use different package versions. Shared installations can affect multiple projects.

Virtual Environment Workflow

Step 1: Create a project folder.

↓

Step 2: Create the virtual environment.

↓

Step 3: Activate the environment.

↓

Step 4: Install project packages using pip.

↓

Step 5: Develop and run the application.

↓

Step 6: Deactivate when finished.

Common Mistakes

  • Installing packages before activating the intended environment.
  • Activating the wrong environment.
  • Forgetting to create a requirements file when dependency sharing is needed.
  • Deleting the environment while it is still active.
  • Confusing the global Python installation with the virtual environment's Python interpreter.

Practical Exercise

Create a folder named python_project and open the terminal inside it.

mkdir python_project

cd python_project

python -m venv venv

Activate the environment according to your operating system.

Then install a package:

pip install requests

Check the installed packages:

pip list

Finally, deactivate the environment:

deactivate

Summary

A virtual environment provides an isolated Python environment for a project. It helps keep dependencies separate and allows different projects to use different package versions. Python provides the venv module to create virtual environments. The basic workflow is to create, activate, install packages, work on the project and deactivate the environment when finished.