import and modules
import is the statement that loads a module, a .py file or an installed package, so your code can use the functions inside it.
Last updated: 30 Sep, 2026 · Python 3.14
Python comes with a large standard library, and every package you install later, from Pydantic to a model SDK, is used the same way: import it, then call what is inside.
Syntax:
import module
from module import nameImporting a standard module
import math
tokens = 1234
print(tokens / 1000)
print(math.ceil(tokens / 1000))1.234 2
import math makes the math module available, and a dot reaches into it. math.ceil rounds up, which is how a price per started thousand is worked out.
Importing names directly
from datetime import date, timedelta
ordered = date(2026, 3, 3)
expected = ordered + timedelta(days=5)
print(expected)
print(date(2026, 3, 10) > expected)2026-03-08 True
from datetime import date, timedelta brings those two names in, so you write date instead of datetime.date. A timedelta is a length of time, and adding it to a date gives a later date.
Importing your own module
Any .py file is a module. Move categorise from Functions into a file called triage.py:
def categorise(text):
text = text.lower()
if "charged" in text or "refund" in text:
return "billing"
if "parcel" in text or "arrived" in text:
return "shipping"
return "other"from triage import categorise
print(categorise("Can I get a refund for the blue mug?"))billing
The file name without .py is the module name. The file must be in the same folder as the code importing it, which is the case here.
Importing a module that is not there
import triage_toolsTraceback (most recent call last):
File "main.py", line 1, in <module>
import triage_tools
ModuleNotFoundError: No module named 'triage_tools'ModuleNotFoundError means Python looked and found no file or installed package with that name.
import module vs from module import name
import math | from math import ceil | |
|---|---|---|
| You write | math.ceil(x) | ceil(x) |
| Clear where it comes from | Yes | Less so in a long file |
| Brings in | The whole module | Only the names listed |
Where imports show up in AI code
- The first lines of every AI program:
import json,from pydantic import BaseModel, a model SDK. - Splitting a project into files, such as tools in one module and the agent in another.
json.py or math.py in your folder is found first, and the real module stops working.Related
- Previous: Functions as values
- Next: Reading and writing files
- Reference: Modules in the Python tutorial
- Print
math.sqrt(144). - Add a function
is_urgent(text)totriage.pyand import both names with a single import statement. - Try
import triage as tand callt.categorise.
Every expert started right here.