← Master Index
Vol. 03 Module 3.4 Lecture

Virtual Environments & pip

Essential Python Skills for AI Engineers (added — needed in practice, not in original outline)

How This Lesson Fits the Module

AI projects accumulate dozens of dependencies: PyTorch, Transformers, LangChain, FastAPI, each with strict version requirements. Installing everything globally leads to conflicts—one project needs torch==2.1, another needs torch==2.4. Virtual environments isolate dependencies per project; pip installs packages into the active environment.

This is prerequisite hygiene before calling external APIs or deploying services covered in the next lectures.

Learning Objectives

By the end of this lesson, students should be able to:

  • Create and activate a Python virtual environment with python -m venv.
  • Install, upgrade, and uninstall packages with pip.
  • Generate and use requirements.txt for reproducible environments.
  • Explain why you should not commit the venv/ folder to git.
  • Recognize modern alternatives: uv, Poetry, and conda environments.
  • Verify which Python and pip are active after activation.

Introduction: Isolated Environments

A virtual environment is a self-contained directory with its own Python interpreter and site-packages. Activating it routes python and pip commands to that isolated space.

# Create environment
python -m venv .venv

# Activate (Windows PowerShell)
.\.venv\Scripts\Activate.ps1

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

# Install dependencies
pip install torch transformers requests python-dotenv

# Freeze versions for teammates
pip freeze > requirements.txt

# Recreate elsewhere
pip install -r requirements.txt
CommandPurpose
python -m venv .venvCreate virtual environment in .venv/
pip install packageInstall into active environment
pip install -r requirements.txtInstall pinned dependency set
pip listShow installed packages
deactivateLeave virtual environment
AI Project Layout
my-rag-app/
  .venv/           # local env (gitignored)
  requirements.txt # pinned deps (committed)
  src/
  .env             # secrets (gitignored)
  README.md

venv + pip

  • Built into Python standard library
  • Simple, universal, minimal tooling
  • requirements.txt for pinning

Poetry / uv / conda

  • Lockfiles, dependency resolution
  • Better for large teams and ML stacks
  • conda common for CUDA-enabled scientific stacks
Common Misconception: “I installed torch globally, so my project will always find it.”

Reality: Without activating the correct environment, you may import the wrong version or none at all. CI/CD and teammates depend on explicit requirements.txt and documented activation steps.

Knowledge Check

  1. Short Answer: What file pins dependency versions for teammates? Answer: requirements.txt (or a lockfile).
  2. True/False: Commit .venv/ to git for convenience. Answer: False—it is large and machine-specific.
  3. Multiple Choice: After activation, which python should point to: (a) system Python, (b) .venv Python, (c) /usr/bin/ruby, (d) none. Answer: (b).
  4. Short Answer: Command to create a venv named .venv? Answer: python -m venv .venv.
  5. True/False: pip freeze > requirements.txt captures installed versions for teammates. Answer: True.
  6. Short Answer: How do you recreate an environment from a pin file? Answer: pip install -r requirements.txt.
  7. Multiple Choice: To leave the active venv: (a) deactivate, (b) rm -rf /, (c) git push, (d) print(). Answer: (a).
  8. True/False: Installing torch globally guarantees every project uses the same correct version. Answer: False—activate the project env or you may import the wrong version.
  9. Short Answer: Name one modern alternative to venv + pip. Answer: uv, Poetry, or conda.
  10. Multiple Choice: requirements.txt should be: (a) gitignored, (b) committed, (c) stored only in RAM, (d) emailed as a screenshot. Answer: (b).

Key Takeaways

  • One virtual environment per project prevents dependency conflicts.
  • pip freeze and requirements.txt enable reproducibility.
  • Never commit .venv/; always document setup in README.
  • Next: Calling REST APIs with packages installed in your environment.
Trainer’s Guide

Setup ritual: Have students create a fresh venv, install requests and python-dotenv, and verify with pip show requests before the next lecture.

Recap: One virtual environment per project plus requirements.txt keeps dependencies reproducible; next, call services with Calling REST APIs (requests).