Skip to content

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.

Beginner 8 min read·Lesson 236 of 306 in Python Programming

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

Terminal
python -m pip install "pandas>=2.2,<4"
python -m pip install "requests[socks]"
python -m pip install --upgrade pandas
python -m pip uninstall pandas
  • Install into an active venv.
  • Quote version specifiers.
  • Record what you install.

Code examples

Typical install commands

Terminal
python -m venv .venv
.venv\Scripts\activate # Windows (source .venv/bin/activate elsewhere)
python -m pip install numpy pandas matplotlib requests
python -m pip list

Upgrading and removing

Terminal
python -m pip install --upgrade requests
python -m pip install "numpy==2.4.6" # switch to an exact version
python -m pip uninstall -y matplotlib

More examples

Checking versions before running

Python
installed = {"requests": "2.32.3", "numpy": "1.26.4"} # e.g. from importlib.metadata
required = {"requests": "2.31", "numpy": "2.0", "rich": "13.0"}
def as_tuple(v):
return tuple(int(p) for p in v.split("."))
for name, minimum in required.items():
have = installed.get(name)
if have is None:
status = "missing"
elif as_tuple(have) < as_tuple(minimum):
status = f"too old ({have} < {minimum})"
else:
status = f"ok ({have})"
print(f"{name:<9}{status}")
Output
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:

TextOutput
requests ok (2.32.3)
numpy too old (1.26.4 < 2.0)
rich missing

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

if "10.0" > "9.0": # False!

Prefer

if (10, 0) > (9, 0):

Strings compare character by character, so '1' < '9' decides the result.

Unquoted specifiers in a shell

Avoid

pip install pandas>=2.2

Prefer

python -m pip install "pandas>=2.2"

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

  1. 1
    Write a script that prints the installed versions of numpy, pandas and requests, or 'not installed'.
  2. 2
    Install 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.