Module 16 · External Libraries and APIs — topic 2 of 12
Installing packages
Installing third-party libraries in practice: pip install with versions and extras, upgrading and uninstalling, checking installed versions, and verifying that the right versions are present before running.
Learning objectives
- Install packages with version constraints and extras
- Upgrade and uninstall packages
- Check installed versions from code
Definition
Installing a package downloads it (and its dependencies) from PyPI and places it in the active environment's site-packages, making it importable. pip handles installation, upgrades and removal.
Introduction
You've chosen a library; now it has to be installed correctly — the right version, in the right environment — so your program and your teammates' copies behave the same.
Why it matters
Most 'works on my machine' bugs come from different package versions. Careful installation and version checks prevent them.
Detailed explanation
Activate the project's virtual environment first, then run python -m pip install name.
Constrain versions with specifiers: name==1.2.3, name>=2,<3. Quote them in shells so < and > aren't treated as redirection.
Some packages offer extras — optional feature sets installed with brackets: python -m pip install "requests[socks]".
python -m pip install --upgrade name upgrades; python -m pip uninstall name removes the package (its dependencies remain).
importlib.metadata.version("name") reports the installed version from code; PackageNotFoundError means it isn't installed.
Compare versions as tuples of integers, or use the packaging library's Version class, never as plain strings ("10.0" < "9.0" as strings).
pip downloads pre-built wheels when available; otherwise it may need to compile the package, which requires build tools.
After installing, record the dependency in requirements.txt (or pyproject.toml) so others can reproduce the environment.
Syntax
- Install into an active venv.
- Quote version specifiers.
- Record what you install.
Code examples
Typical install commands
Upgrading and removing
More examples
Checking versions before running
requests ok (2.32.3) numpy too old (1.26.4 < 2.0) rich missing
Step-by-step explanation
- A real script would fill installed using importlib.metadata.version for each name.
- Versions are compared as tuples of integers, so 1.26.4 is correctly older than 2.0.
- Each requirement gets a clear status that tells the user what to install or upgrade.
Expected output
Running “Checking versions before running” prints exactly:
Practical applications
- Startup checks in data tools that need recent library features.
- Setup scripts that install exactly the versions a project was tested with.
Best practices
- Always install inside a virtual environment.
- Quote specifiers and pin application versions.
- Check critical versions at startup.
Common mistakes
Comparing versions as strings
Avoid
Prefer
Strings compare character by character, so '1' < '9' decides the result.
Unquoted specifiers in a shell
Avoid
Prefer
The shell treats > as output redirection and creates a file named =2.2.
Important notes
- python -m pip index versions name lists available versions of a package.
Key takeaways
- Install with python -m pip install in an active venv.
- Specifiers and extras refine what is installed.
- importlib.metadata reports installed versions.
Try it yourself
- 1Write a script that prints the installed versions of numpy, pandas and requests, or 'not installed'.
- 2Install a package with an extra and list what additional packages it brought in.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
Test yourself across the whole course in the Python practice bank.