Virtual Environments (venv)
Virtual environments isolate project dependencies. Each project gets its own Python packages, preventing version conflicts between projects.
15 min•By Priygop Team•Updated 2026
Virtual Environments
Virtual Environments
# Why virtual environments?
# Project A needs requests==2.28
# Project B needs requests==2.31
# Without venv, they conflict!
# Creating a virtual environment:
# python -m venv myenv # create
# myenv\Scripts\activate # activate (Windows)
# source myenv/bin/activate # activate (macOS/Linux)
# deactivate # deactivate
# Workflow:
# 1. Create project directory
# 2. Create venv: python -m venv venv
# 3. Activate: venv\Scripts\activate
# 4. Install packages: pip install requests
# 5. Save deps: pip freeze > requirements.txt
# 6. Deactivate when done: deactivate
# requirements.txt example:
requirements = """requests==2.31.0
beautifulsoup4==4.12.2
python-dotenv==1.0.0
pytest==7.4.3"""
print("=== requirements.txt ===")
print(requirements)
# Recreate environment:
# pip install -r requirements.txt
# .gitignore — always ignore venv!
gitignore = """# Virtual environment
venv/
.env
__pycache__/
*.pyc"""
print("\n=== .gitignore ===")
print(gitignore)Tip
Tip
Create a .venv folder per project. Add .venv to .gitignore. Share requirements.txt, not the venv itself.
Diagram
Loading diagram…
venv for simple projects. conda for data science/ML. poetry for modern dependency management.
Common Mistake
Warning
Committing the venv folder to git. It contains platform-specific binaries. Only commit requirements.txt.
Quick Quiz
Practice Task
Note
(1) Create a venv: python -m venv .venv. (2) Activate it. (3) Install a package. (4) Generate requirements.txt.