import statement
import loads a module so you can use its functions, such as math.sqrt() or random.choice(), from the standard library or your own files.
import module
import module as alias
from module import name, other_name
from module import name as aliasA module is a Python file full of ready-made functions and values. import math loads the math module, and you then reach its contents with a dot: math.sqrt(16).
Python ships with a large standard library: math, random, datetime, json, collections and many more, all available without installing anything. Your own .py files can be imported the same way, by file name without .py.
from math import sqrt brings one name straight into your file, so you can write sqrt(16). Avoid from module import *: it fills your file with names you cannot see and may overwrite your own.
Examples
1import a whole module
import math
print(math.sqrt(81))
print(math.pi)
print(math.ceil(4.2), math.floor(4.8))9.0 3.141592653589793 5 4
2from ... import and as
from datetime import date, timedelta
import json as js
start = date(2026, 3, 14)
due = start + timedelta(days=30)
print(due.isoformat())
print(js.dumps({"due": due.isoformat(), "paid": False}))2026-04-13
{"due": "2026-04-13", "paid": false}as gives a module or name a shorter or clearer name in your file.
3Random numbers you can repeat
random.seed() makes the sequence repeatable: the same seed always gives the same numbers.
import random
random.seed(7)
first = [random.randint(1, 6) for _ in range(5)]
random.seed(7)
second = [random.randint(1, 6) for _ in range(5)]
print(first == second)
print(len(first), all(1 <= n <= 6 for n in first))True 5 True
4Count things with collections
from collections import Counter
votes = ["pizza", "sushi", "pizza", "tacos", "pizza", "sushi"]
counts = Counter(votes)
print(counts.most_common(2))
print(counts["tacos"])[('pizza', 3), ('sushi', 2)]
15Days between two dates
from datetime import date
booked = date(2026, 6, 1)
trip = date(2026, 8, 15)
print((trip - booked).days, "days to go")75 days to go
Common mistakes
Naming your file like a module
If your own file is called random.py or math.py, import random loads your file instead of the real module, and nothing works. Give your files other names.
Forgetting the module name
After import math, the function is math.sqrt, not sqrt. Either write the prefix, or use from math import sqrt.
import math
try:
print(sqrt(25))
except NameError:
print("NameError: sqrt is not defined")
print(math.sqrt(25))NameError: sqrt is not defined 5.0
Questions people ask
What is the difference between import x and from x import y?
import x keeps everything behind the x. prefix. from x import y puts only y in your file, without a prefix.
How do I import my own file?
Put helpers.py in the same folder and write import helpers, then call helpers.my_function().
How do I install a module that is not in the standard library?
Use pip in a terminal: pip install package-name, then import it as usual.
Where should import lines go?
At the top of the file, one per line, standard library first, then other packages, then your own modules.