Lesson 12: Virtual Environments & Packages

⏱ ~35 min Lesson 12 of 14 💚 Free

Real Python projects use third-party packages (libraries). Virtual environments keep each project's packages isolated so they don't conflict. This is standard professional practice.

Key Concepts

Why Packages?

Python's standard library is powerful, but for data science you'll want pandas, for web requests requests, for machine learning scikit-learn. These are installed from PyPI (the Python Package Index) using pip.

Virtual Environments

python -m venv venv # creates a virtual env
source venv/bin/activate # Mac/Linux
venv\Scripts\activate # Windows
pip install requests pandas
Each project gets its own isolated set of packages.

requirements.txt

pip freeze > requirements.txt # saves current packages
pip install -r requirements.txt # installs from list
Commit requirements.txt to git so others can reproduce your environment exactly.

Popular Packages

requests: HTTP/API calls. pandas: data analysis. numpy: math/arrays. matplotlib: charts. flask: web apps. pytest: testing. black: code formatting. All installed with pip install .

✅ Check Your Understanding

1. What command creates a virtual environment?

2. What is requirements.txt used for?

3. Where are Python packages hosted for download?