Lesson 35 of 55
PythonVirtual Environments and pip
20 minutes
Course progress64%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 35 of 55
Python20 minutes
Keep these ideas in mind as you work through the lesson.
A virtual environment isolates a project's Python interpreter and its third‑party packages, preventing version conflicts across projects.
# Create a virtual environment named .venv
python -m venv .venv
# Activate (Linux/macOS)
source .venv/bin/activate
# Activate (Windows)
.\\venv\\Scripts\\activate
# Upgrade pip inside the env
pip install --upgrade pip
# Install a library, e.g., requests
pip install requests
# Freeze exact versions for reproducibility
pip freeze > requirements.txt
Whenever you start work on the project, activate the environment first. Deactivate with the deactivate command or simply close the terminal.