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()orsqrt()) - Class: A blueprint for creating objects (we’ll cover this later)
- 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
import math- brings in the entire math toolboxfrom 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:Installing packages
External packages need installation:Sharing your project: requirements.txt
When you share your Python project, others need to know which packages to install. The standard way is using arequirements.txt file:
Creating requirements.txt
List all your project’s packages:Installing from requirements.txt
When someone gets your project, they run: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]”
Common mistakes
Import errors
Import errors
Name conflicts
Name conflicts
What’s next?
Learn to connect to APIs and fetch data from the internet!Working with APIs
Connect to online services