Skip to main content

Using packages

Python packages add new functionality to your programs. There are two types:
  • Built-in: Come with Python (no installation needed)
  • External: Need to install first with pip

Understanding the terminology

Let’s clarify what these terms mean:
  • Module: A single Python file (like math.py)
  • Package: A folder containing multiple modules
  • Function: A reusable block of code (like print() or sqrt())
  • Class: A blueprint for creating objects (we’ll cover this later)
Think of it like this:
  • A module is like a toolbox
  • A package is like a garage with multiple toolboxes
  • A function is like a specific tool (hammer, screwdriver)
  • A class is like a blueprint for building tools

Import patterns explained

What’s happening:
  • import math - brings in the entire math toolbox
  • from math import sqrt - takes just the sqrt tool from the math toolbox

Built-in modules

Python includes many useful modules:

Common built-in modules

Import methods

Different ways to import:
Avoid from module import * as it can cause naming conflicts and makes code harder to understand.

Installing packages

External packages need installation:
Always ensure your virtual environment is activated before installing! This is the #1 source of import errors. If you get “ModuleNotFoundError” after installing, you probably installed to the wrong environment. Learn more about virtual environments.

Sharing your project: requirements.txt

When you share your Python project, others need to know which packages to install. The standard way is using a requirements.txt file:

Creating requirements.txt

List all your project’s packages:
This creates a file like:

Installing from requirements.txt

When someone gets your project, they run:
This installs all the packages at once!
Later in the course, we’ll learn about uv - a modern, faster alternative to pip that makes package management even easier.

Using external packages

After installation, import and use:
Always use virtual environments for projects. They prevent package conflicts between different projects.

Finding packages

Where to find packages:
  • PyPI - Official Python package index
  • Awesome Python - Curated list
  • ChatGPT - Ask “What Python package should I use for [task]?” - Great for recommendations and comparisons
  • Google “Python package for [task]”
ChatGPT is excellent for finding packages. Try asking: “What’s the best Python package for reading Excel files?” or “Compare pandas vs polars for data analysis.” It can explain which package to use and why.

Common mistakes

What’s next?

Learn to connect to APIs and fetch data from the internet!

Working with APIs

Connect to online services