Python Virtual Env
What is a virtual environment?
- An isolated Python environment
- Keeps project dependencies separate
- Prevents breaking system Python
- Required on modern Linux (PEP 668)
1️⃣ Create a virtual environment
python3 -m venv venv_name(.venv)
venv= folder name (can be anything)- Creates isolated Python + pip
2️⃣ Activate the virtual environment
Linux / macOS
source venv_name/bin/activate
Windows
venv_name\\Scripts\\activate
✔ Prompt shows (venv) when active
3️⃣ Deactivate the environment
deactivate
4️⃣ Install packages (inside venv)
pip install package_name
Example:
pip install requests PyYAML
✔ No sudo
✔ No --user
✔ No system impact
5️⃣ Check which Python / pip is used
which python
which pip
Or:
python --version
pip --version
6️⃣ Save dependencies
pip freeze > requirements.txt
7️⃣ Restore dependencies
pip install -r requirements.txt
8️⃣ Remove a virtual environment
deactivate # if active
rm -rf venv
(No uninstall needed)
9️⃣ Common mistakes (remember this 🚨)
❌ sudo pip install ...
❌ pip install --break-system-packages
❌ Using system Python for projects
✔ Always use a venv for projects
🔁 Typical daily workflow
python3 -m venv venv_name
source venv_name/bin/activate
pip install -r requirements.txt
python app.py
deactivate
🧠 One-line memory trick
Create → Activate → Install → Run → Deactivate
Bonus: Quick uv equivalent (modern)
uv venv_name
source .venv_name/bin/activate
uv pip install requests