Sub topic 1
What the standard library gives you and how to import it
Prerequisites
This chapter assumes you can do the following:
- Write basic Python: variables, functions, and control flow.
- Work with lists and strings.
- Run a script or use the interactive interpreter.
What the standard library is
The standard library is the set of modules that ship with Python. A module is a file of Python code that defines functions, classes, and constants. You don't install anything with pip to use these modules. They are there as soon as Python is.
This tutorial covers three of them:
| Module | What it provides |
|---|---|
| `math` | Constants and numeric functions: roots, logarithms, trigonometry, rounding, counting |
| `random` | Pseudo-random numbers and random choices from sequences |
| `datetime` | Dates, times, durations, formatting, and time zones |
Python loads a module only when you ask for it with import. Your program starts small, and each name you use is traceable to the module it came from.
The three import forms
The most common form imports the whole module. You reach its contents with a dot:
import math
print(math.sqrt(25)) # 5.0
print(math.pi) # 3.141592653589793The second form pulls specific names into your file. You then use them without the prefix:
from math import sqrt, pi
print(sqrt(25)) # 5.0
print(pi) # 3.141592653589793The third form gives the module a shorter alias:
import datetime as dt
print(dt.date(2026, 11, 26)) # 2026-11-26Choose by readability. math.sqrt(x) shows where sqrt comes from. from math import sqrt is shorter but hides that. If you use a name only once or twice, the prefixed form is usually clearer.
Exploring a module
You don't need to memorize what a module contains. Two built-in functions help you look:
import math
print(math.sqrt.__doc__) # short description of sqrt
print("floor" in dir(math)) # Truedir(module) lists the names a module defines. help(math.floor) prints the documentation for one function. Both work in a script or in the interactive interpreter.
Common errors
Some import mistakes come up often enough to know in advance.
- Wildcard imports.
from math import *dumps every public name into your file. It can silently replace names you defined earlier, and readers can't tell where a name came from. Avoid it. - Confusing a module with a class of the same name. The
datetimemodule contains a class that is also calleddatetime. Afterimport datetime, you must writedatetime.datetime.now(). Afterfrom datetime import datetime, you writedatetime.now(). Mixing the two styles producesAttributeError: module 'datetime' has no attribute 'now'. - Shadowing the module with your own file. If you save a script as
random.pyormath.py, Python may import your file instead of the standard one. You then get errors such asAttributeError: partially initialized module 'random' has no attribute 'randint'. Rename your file and delete any__pycache__folder next to it. - Misspelled module names. A typo such as
import mathsraisesModuleNotFoundError. Check the spelling before you assume something needs installing.
Checking your understanding
Exercise
A script contains import datetime followed by print(datetime.now()). It fails with an AttributeError. Give two different ways to fix it.
Show answer
Either call the class through the module with import datetime and print(datetime.datetime.now()), or import the class directly with from datetime import datetime and keep print(datetime.now()). The error occurs because datetime is the module in the first version, and the module has no now function. The class of the same name does.