Lesson 2 of 20

Environment Setup

Virtual Environments

3 min read

Virtual environments are isolated Python environments that keep your project's dependencies separate from other projects and your system Python.

Why Use Virtual Environments?

Imagine two AI projects:

  • Project A needs openai==1.0.0
  • Project B needs openai==1.5.0

Without virtual environments, installing one would break the other. Virtual environments solve this by creating isolated spaces for each project.

Creating a Virtual Environment

# Navigate to your project folder
cd my-ai-project

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

This creates a venv folder containing:

  • A copy of the Python interpreter
  • A place for installed packages
  • Activation scripts

Activating the Environment

macOS/Linux

source venv/bin/activate

Windows (Command Prompt)

venv\Scripts\activate.bat

Windows (PowerShell)

venv\Scripts\Activate.ps1

When activated, you'll see (venv) at the start of your terminal prompt.

Deactivating

When you're done working:

deactivate

Best Practices

PracticeReason
One venv per projectPrevents dependency conflicts
Name it venv or .venvStandard convention, easy to gitignore
Don't commit venv to gitIt's large and system-specific
Recreate from requirements.txtShare dependencies, not the environment

Next, we'll learn how to manage packages with pip. :::

Quick check: how does this lesson land for you?

Quiz

Module 1: Environment Setup

Take Quiz
FREE WEEKLY NEWSLETTER

Stay on the Nerd Track

One email per week — courses, deep dives, tools, and AI experiments.

No spam. Unsubscribe anytime.