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.txtfor 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
| Command | Purpose |
|---|---|
python -m venv .venv | Create virtual environment in .venv/ |
pip install package | Install into active environment |
pip install -r requirements.txt | Install pinned dependency set |
pip list | Show installed packages |
deactivate | Leave virtual environment |
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.txtfor pinning
Poetry / uv / conda
- Lockfiles, dependency resolution
- Better for large teams and ML stacks
- conda common for CUDA-enabled scientific stacks
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
- Short Answer: What file pins dependency versions for teammates? Answer:
requirements.txt(or a lockfile). - True/False: Commit
.venv/to git for convenience. Answer: False—it is large and machine-specific. - Multiple Choice: After activation,
which pythonshould point to: (a) system Python, (b).venvPython, (c) /usr/bin/ruby, (d) none. Answer: (b). - Short Answer: Command to create a venv named
.venv? Answer:python -m venv .venv. - True/False:
pip freeze > requirements.txtcaptures installed versions for teammates. Answer: True. - Short Answer: How do you recreate an environment from a pin file? Answer:
pip install -r requirements.txt. - Multiple Choice: To leave the active venv: (a)
deactivate, (b)rm -rf /, (c)git push, (d)print(). Answer: (a). - 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.
- Short Answer: Name one modern alternative to venv + pip. Answer: uv, Poetry, or conda.
- Multiple Choice:
requirements.txtshould 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 freezeandrequirements.txtenable reproducibility.- Never commit
.venv/; always document setup in README. - Next: Calling REST APIs with packages installed in your environment.
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).