Virtual Environments

Learn why and how to create isolated Python environments.

Why create virtual environments?

Environments isolate project dependencies. This prevents conflicts between projects that require different versions of the same library. It makes your project self-contained and reproducible.

Python has a built-in module called `venv` for creating virtual environments.

Creating and Activating a venv

# Create a virtual environment in a folder named 'venv'
python3 -m venv venv

# Activate it (macOS/Linux)
source venv/bin/activate

# On Windows, use:
# .\venv\Scripts\activate
            

Once activated, your terminal prompt will change to show the environment's name. Any packages you install will be local to this environment.