Python ยท Chapter 25 of 45

Packages & pip

A PACKAGE is a folder of modules with an `__init__.py`. Third-party packages are installed from the Python Package Index (PyPI) using `pip`.

Examples: `requests`, `numpy`, `pandas`, `flask`.

Installing

Run `pip install package` in your terminal. Use `pip install -r requirements.txt` to install from a list file.

requirements.txt

A plain text file listing dependencies (one per line, optionally pinned to a version). Commit it to version control.

Example 1 (bash)
pip install requests
python -c "import requests; print(requests.get('https://api.github.com').status_code)"
Output
200

Install a package then use it.

Example 2 (text)
requests>=2.31
numpy
pandas==2.2.*

A typical requirements.txt file.

Key points

  • `pip` is Python's package installer.
  • PyPI is the package repository.
  • Use `requirements.txt` to pin dependencies.
  • Always install into a virtual environment (see next topic).
๐Ÿ’ก Note: Use `pip freeze > requirements.txt` to snapshot the exact versions currently installed.

๐Ÿ“ Quick Quiz

1. PyPI stands for:

2. Which command installs a package?

3. requirements.txt is used to: