Key Takeaways: Libraries and Virtual Environments

One-Sentence Summary

Virtual environments isolate each project's packages so they can't conflict with each other, and requirements.txt records exactly what you installed so anyone can recreate your environment.

The Virtual Environment Workflow

Step Command What It Does
Create python -m venv .venv Makes an isolated Python installation in .venv/
Activate source .venv/bin/activate (macOS/Linux) or .venv\Scripts\activate (Windows) Points python and pip to the environment
Install pip install package_name Installs into the environment (not system Python)
Freeze pip freeze > requirements.txt Records all installed packages and exact versions
Recreate pip install -r requirements.txt Installs everything from the requirements file
Deactivate deactivate Returns to system Python

Core pip Commands

Command Purpose
pip install X Install package X (latest version)
pip install X==1.2.3 Install exact version 1.2.3
pip install "X>=1.0,<2.0" Install within a version range
pip install --upgrade X Upgrade to latest version
pip uninstall X Remove package X
pip list Show all installed packages (human-readable)
pip freeze Show all installed packages (requirements.txt format)
pip show X Show details about package X
pip install -r requirements.txt Install from requirements file

Semantic Versioning (MAJOR.MINOR.PATCH)

Change Meaning Your code...
MAJOR (1.x → 2.0) Breaking changes May need updates
MINOR (2.3 → 2.4) New features, backward compatible Should still work
PATCH (2.3.0 → 2.3.1) Bug fixes only Will still work

requirements.txt vs. pyproject.toml

Feature requirements.txt pyproject.toml
Format Plain text TOML (structured)
Typical versioning Exact pins (==) Ranges (>=,<)
Project metadata None Name, version, description
Best for Applications, quick projects Libraries, publishable packages
Dev dependencies Separate file Built-in section

Package Evaluation Checklist (Quick Version)

Before pip install, check: 1. Downloads — Is it widely used? 2. Maintenance — When was the last update? 3. Source code — Can you read it? Is it on GitHub? 4. Documentation — Is it clear and current? 5. License — Is it compatible with your project? 6. Dependencies — How many does it pull in?

Common Pitfalls

Pitfall Symptom Fix
Forgot to activate venv ModuleNotFoundError for installed packages Run source .venv/bin/activate (or Windows equivalent)
Installed to system Python Package works without venv, not inside it Reinstall inside the activated environment
No requirements.txt Collaborators can't run your code Run pip freeze > requirements.txt
Committed .venv/ to git Bloated repository, platform-specific files Add .venv/ to .gitignore
Unpinned versions Builds break months later Always pin with pip freeze

Key Libraries by Domain

Domain Go-to Library One-Line Description
HTTP/APIs requests Make HTTP requests simply
Data analysis pandas DataFrames for tabular data
Visualization matplotlib Charts and plots
Web apps flask Lightweight web framework
Terminal UI rich Beautiful terminal formatting
Testing pytest Modern test framework
Bioinformatics biopython Biological sequence analysis

TaskFlow Progress

v2.2: Added a virtual environment (.venv), requirements.txt, and the rich library for colorful terminal output. Task list now displays as a formatted table with color-coded priorities.

What's Next

Chapter 24: Use your new environment skills to set up a web scraping project with beautifulsoup4 and build an automation pipeline.