PYTHON FOR AI • LESSON 1

Virtual Environments

Python projects often need different packages and different package versions. A virtual environment keeps the dependencies of one project separated from other projects.

CORE IDEA

A virtual environment gives each Python project its own isolated environment.

Instead of installing every Python package globally, you create a separate environment for a project. Packages installed inside that environment belong to that project.

01

Why Do We Need Virtual Environments?

Imagine you have two Python projects.

Project A needs one version of a package:

Project A
numpy 1.x

But Project B needs another version:

Project B
numpy 2.x

If both projects use the same global Python environment, package versions can conflict with each other.

This is especially common in AI projects because AI applications use many packages such as NumPy, Pandas, PyTorch, TensorFlow, and other libraries.

02

Simple Real-World Example

Think about two kitchens.

Kitchen A
├── Ingredients for Project A
└── Tools for Project A


Kitchen B
├── Ingredients for Project B
└── Tools for Project B

The kitchens are separate, so changing something in Kitchen A does not affect Kitchen B.

A Python virtual environment works in a similar way. Each project gets its own isolated set of packages.

03

Create a Virtual Environment

Python provides the venv module for creating virtual environments.

First create a project directory:

mkdir ai-project

cd ai-project

Now create a virtual environment:

python -m venv venv

This creates a directory called venv.

ai-project/
│
└── venv/

The environment contains its own Python executable and package installation area.

04

Activate the Environment

On Linux or macOS, activate the environment with:

source venv/bin/activate

After activation, your terminal usually shows the environment name:

(venv) user@computer:~/ai-project$

The (venv) tells you that the virtual environment is currently active.

05

Activate on Windows

On Windows Command Prompt:

venv\Scripts\activate

On Windows PowerShell:

venv\Scripts\Activate.ps1

After activation, you should see (venv) in the terminal.

06

Install Packages Inside the Environment

Once the environment is activated, install packages using pip.

pip install numpy

You can install multiple packages:

pip install numpy pandas matplotlib

These packages are installed into the active virtual environment rather than being added to your project independently of the environment.

07

Check Installed Packages

You can see the packages installed in the current environment using:

pip list

You can also check a specific package:

pip show numpy

This can show information such as the installed version and installation location.

08

requirements.txt

A project often needs a list of the packages it depends on. Python projects commonly store this information in a file called requirements.txt.

Create it using:

pip freeze > requirements.txt

The file might contain:

numpy==2.x.x
pandas==2.x.x
matplotlib==3.x.x

Another developer can then install those dependencies with:

pip install -r requirements.txt

This makes it much easier to recreate the same project environment.

09

Deactivate the Environment

When you finish working on the project, you can leave the virtual environment using:

deactivate

The (venv) indicator will disappear from your terminal.

10

Complete Virtual Environment Workflow

A typical Python AI project can follow this workflow:

# 1. Create project
mkdir ai-project

# 2. Enter project
cd ai-project

# 3. Create environment
python -m venv venv

# 4. Activate environment
source venv/bin/activate

# 5. Install packages
pip install numpy pandas

# 6. Save dependencies
pip freeze > requirements.txt

# 7. Work on your project

# 8. Leave environment
deactivate
11

Virtual Environments in AI Projects

AI projects usually depend on many libraries. For example:

ai-project/
│
├── venv/
│
├── models/
│   └── model.py
│
├── data/
│   └── data.csv
│
├── main.py
│
└── requirements.txt

The venv directory contains the isolated environment, while your actual application code lives in files such as main.py and the models directory.

In a real project, you normally do not commit the entire venv directory to Git. Instead, you commit files such as requirements.txt so another developer can recreate the environment.

12

Why Virtual Environments Matter for AI

AI projects can have complicated dependencies. Different projects may require different versions of the same library.

Without Virtual Environment With Virtual Environment
Packages can conflict Projects stay isolated
Global Python can become messy Each project has its own dependencies
Harder to reproduce projects requirements.txt can recreate dependencies
Changing one project can affect another Projects are separated
KEY TAKEAWAY

One project, one isolated environment.

A virtual environment keeps a project's Python packages separate from other projects. The basic workflow is: create the environment, activate it, install packages, work on the project, and deactivate it when finished. In AI development, this becomes essential because projects often depend on many packages and specific package versions.