🐍 Lesson 26: Virtual Environments
1. Virtual Environment ဆိုတာဘာလဲ?
မြန်မာ → Virtual Environment (အတိုကွေ့ venv) ဆိုတာ Python project တစ်ခုချင်းစီအတွက် သီးသန့် Python environment တစ်ခုဖန်တီးပေးတဲ့ နည်းလမ်း။ Project တစ်ခုမှာ သုံးတဲ့ library version နဲ့ အခြား project တစ်ခုမှာ သုံးတဲ့ version မတူနိုင်တယ်။
English → A virtual environment is an isolated Python environment for each project, ensuring dependencies don't conflict between projects.
2. Why Use Virtual Environments?
- Project တစ်ခုချင်းစီအတွက် dependency သီးသန့်ထိန်းချုပ်နိုင်တယ်
- Version conflict မဖြစ်အောင် ကာကွယ်နိုင်တယ်
- Production server, testing environment တို့မှာ အတိအကျ အလုပ်လုပ်နိုင်မယ်
3. အကျဉ်းချုပ်
✅ 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)