🐍 Lesson 26: Virtual Environments
1. What is a Virtual Environment?
In short → A virtual environment (venv for short) is a way to create a dedicated Python environment for each project. The library version one project needs might not match what another project needs.
In other words → A virtual environment is an isolated Python environment for each project, ensuring dependencies don't conflict between projects.
2. Why Use Virtual Environments?
- Lets you manage dependencies separately for each project
- Prevents version conflicts
- Makes sure your code behaves exactly the same on production servers and testing environments
3. Summary
✅ Virtual environment = project-specific Python environment
✅ python -m venv myenv → create
✅ activate / deactivate → switch on/off
✅ pip freeze > requirements.txt → dependency record
python
# ===== 1. Create Virtual Environment =====
print("===== Create Virtual Environment =====")
print("# Command: python -m venv myenv")
print("# Creates 'myenv' folder with isolated Python")
# ===== 2. Activate Virtual Environment =====
print(f"\n===== Activate (Windows) =====")
print("# Command: myenv\Scripts\activate")
print("# Terminal shows: (myenv)")
print(f"\n===== Activate (Mac/Linux) =====")
print("# Command: source myenv/bin/activate")
print("# Terminal shows: (myenv)")
# ===== 3. Install Packages =====
print(f"\n===== Install in Virtual Environment =====")
print("# After activation, install packages:")
print("# pip install requests")
print("# pip install flask")
print("# Packages installed only in this venv")
# ===== 4. Requirements File =====
print(f"\n===== Freeze Dependencies =====")
print("# Command: pip freeze > requirements.txt")
print("# Saves all installed packages with versions")
print(f"\n===== Install from Requirements =====")
print("# Command: pip install -r requirements.txt")
print("# Recreates exact environment")
# ===== 5. Deactivate =====
print(f"\n===== Deactivate =====")
print("# Command: deactivate")
print("# Exits virtual environment")
# ===== 6. Benefits =====
print(f"\n===== Benefits =====")
print("✅ Isolated dependencies per project")
print("✅ No version conflicts")
print("✅ Reproducible environments")
print("✅ Easy to share (requirements.txt)")You should see
===== Create Virtual Environment ===== # Command: python -m venv myenv # Creates 'myenv' folder with isolated Python ===== Activate (Windows) ===== # Command: myenvScriptsactivate # Terminal shows: (myenv) ===== Activate (Mac/Linux) ===== # Command: source myenv/bin/activate # Terminal shows: (myenv) ===== Install in Virtual Environment ===== # After activation, install packages: # pip install requests # pip install flask # Packages installed only in this venv ===== Freeze Dependencies ===== # Command: pip freeze > requirements.txt # Saves all installed packages with versions ===== Install from Requirements ===== # Command: pip install -r requirements.txt # Recreates exact environment ===== Deactivate ===== # Command: deactivate # Exits virtual environment ===== Benefits ===== ✅ Isolated dependencies per project ✅ No version conflicts ✅ Reproducible environments ✅ Easy to share (requirements.txt)