Intermediate·14 min·0/47 done

Python Packages and pip

Learn what packages are, how pip installs them, and how requirements files keep projects repeatable.

Why it matters: AI projects depend on packages such as NumPy, pandas, scikit-learn, PyTorch, and FastAPI.

Basics

Modules and Packages

A module is one Python file. A package is a folder of related modules.

Python includes standard-library packages such as json. Third-party packages add tools such as NumPy and pandas.

Use import package_name after a package is available.

Tiny example

Check whether Python can find a package.

import importlib.util

name = "json"
found = importlib.util.find_spec(name) is not None
print(name, "available:", found)

Basics

Install with pip

pip is Python's package installer.

  • Install: python -m pip install pandas
  • Upgrade: python -m pip install --upgrade pandas
  • List: python -m pip list
  • Remove: python -m pip uninstall pandas

The browser lab loads supported packages automatically, so students can practise here without setup.

Tiny example

See the common pip commands as a small list.

commands = [
    "python -m pip install pandas",
    "python -m pip list",
]
for command in commands:
    print(command)

Basics

Requirements Files

A requirements.txt file lists the packages a project needs.

Pin versions with == when repeatable builds matter. Install the full list with python -m pip install -r requirements.txt.

Common mistake: installing packages without recording their versions.

Tiny example

Create a short repeatable package list.

requirements = [
    "numpy==2.1.3",
    "pandas==2.2.3",
]
print("\n".join(requirements))

Then AI / ML

AI / ML examples

Same idea, used in real AI work. Press Try in lab to run it.

AI / ML example

AI / ML: check project dependencies

Check which common data packages are available.

In AI / ML: Training and data projects usually depend on NumPy, pandas, and scikit-learn.

example.py

Python

import importlib.util

packages = ["numpy", "pandas", "sklearn"]
for name in packages:
    available = importlib.util.find_spec(name) is not None
    print(name, "available:", available)

AI / ML example 2

AI / ML: build a requirements list

Keep one readable list of project dependencies.

In AI / ML: Model services pin data, model, and API libraries before deployment.

example.py

Python

project_packages = {
    "numpy": "2.1.3",
    "pandas": "2.2.3",
    "scikit-learn": "1.5.2",
}
for name, version in project_packages.items():
    print(f"{name}=={version}")

Takeaways

  • 1A module is one file; a package groups modules.
  • 2pip installs and manages third-party packages.
  • 3Requirements files make projects easier to rebuild.
Certificate