Velamuvi AIAcademy

Lesson 14 of 16 · Project Setup

Installing Packages With pip

Learn to install, upgrade and remove Python packages with pip, pin versions, and share your project's dependencies with a requirements.txt file.

  • 8 min read
  • Intermediate
  • Reviewed
In this lesson you will learn
  • What a package is and where pip gets it from
  • How to install, upgrade, list and remove packages
  • How version numbers and version rules work
  • How to share a project's packages with requirements.txt

Packages and PyPI

Python comes with a large standard library: json, csv, datetime and many more. Everything else is a package written by someone else and published on the Python Package Index, known as PyPI (pypi.org). It holds hundreds of thousands of packages, including NumPy, pandas, PyTorch and the official clients for AI APIs.

pip is the tool that downloads packages from PyPI and installs them into your environment. It comes with Python.

Installing a package

python -m pip install requests

pip downloads requests, plus any packages that requests itself needs, and installs them. Now your code can use it:

import requests

response = requests.get("https://api.github.com")
print(response.status_code)

Writing python -m pip instead of plain pip makes sure you use the pip that belongs to the active Python. Both usually work inside an activated environment, but python -m pip never surprises you.

The everyday commands

CommandWhat it does
python -m pip install pandasInstall a package
python -m pip install pandas numpyInstall several at once
python -m pip install --upgrade pandasUpgrade to the newest version
python -m pip listShow everything installed
python -m pip show pandasDetails of one package, including its version
python -m pip uninstall pandasRemove a package

Version numbers

Most packages use three numbers: major.minor.patch, such as 2.1.3.

  • Patch (2.1.3 to 2.1.4): bug fixes only.
  • Minor (2.1 to 2.2): new features, old code keeps working.
  • Major (2 to 3): big changes that may break your code.

You can ask pip for a particular version, or a range:

python -m pip install "pandas==2.2.3"    # exactly this version
python -m pip install "pandas>=2.2"      # this version or newer
python -m pip install "pandas~=2.2.0"    # 2.2.x only: patches, no new features

The version numbers here are only examples. Use quotation marks around the requirement, because some terminals treat > and < as special characters.

Is a package installed?

From Python, importlib.util.find_spec() tells you whether a module can be imported, without importing it.

Try it yourselfPython
import importlib.util

for name in ["json", "csv", "velamuvi_not_a_real_package"]:
    found = importlib.util.find_spec(name) is not None
    print(f"{name}: {'available' if found else 'not installed'}")

json and csv are in the standard library, so they are always available. The made-up name is not installed anywhere.

Sharing your packages: requirements.txt

When someone else (or you, on another computer) wants to run your project, they need the same packages. The convention is a file called requirements.txt that lists them, one per line.

Create it from your current environment:

python -m pip freeze > requirements.txt

pip freeze prints every installed package with its exact version, and > saves that list into the file. It looks something like this:

certifi==2025.8.3
charset-normalizer==3.4.3
idna==3.10
requests==2.32.5
urllib3==2.5.0

Anyone can then recreate the same environment in two steps:

python -m venv .venv
# activate it, then:
python -m pip install -r requirements.txt

Unlike the .venv folder, requirements.txt is small and belongs in Git.

Common mistakes

Practice exercise

Read a small requirements.txt and show what each line asks for.

Your solutionPython
requirements = """
numpy==2.1.0
pandas>=2.2

# testing
pytest
"""

# Print each package and its version rule, one per line.
# Skip blank lines and comment lines (they start with #).
# A package without a rule should say "any version".
# Output must be exactly:
# numpy: ==2.1.0
# pandas: >=2.2
# pytest: any version
Knowledge check

What does python -m pip install -r requirements.txt do?

Summary

  • Packages live on PyPI, and pip installs them into the active environment.
  • Use python -m pip install, --upgrade, list, show and uninstall.
  • Versions follow major.minor.patch. Pin them with ==, or allow a range with >= or ~=.
  • pip freeze > requirements.txt records your packages, and pip install -r requirements.txt recreates them.

Next: organising a project into files and folders.

Finished reading? Tick it off to track your progress.