Python Modules and Imports: import, from and as
Use thousands of ready-made functions from Python's standard library, or split your own code into files.
- import module and module.function
- from module import name
- Aliases with as
import math
- math.sqrt(16)
- clear where names come from
from math import pi
- pi
- short, but watch for name clashes
import random as rnd
- rnd.randint(1, 6)
- a short alias
Borrow code from another file
The problem
Many tasks have already been solved well: square roots, random numbers, dates, counting items. Writing them yourself takes time and invites bugs. Python ships with a large standard library, and import lets you use it in one line.
Think of it like this
Importing a module is like borrowing a toolbox from a shared workshop. You bring the whole box (import math) and pick tools from it by name (math.sqrt), or take just one tool (from datetime import date). You can also put a shorter label on the box (as).
import math🧰 Borrow the whole toolboxmath.sqrt(16)🔧 Use one tool from the boxfrom datetime import date🪛 Take only the tool you needCounter as Tally🏷️ Put your own label on itA module is a file of Python code, and import makes its functions and classes available in your program.
Key points
import mathkeeps the module name as a prefix, so you writemath.sqrt(16)andmath.pi.from datetime import datebrings one name in directly, so you writedate(...).from collections import Counter as Tallygives the imported name a different name.random.seed(1)makes the 'random' results repeat on every run, which helps testing.
When should you use it?
- Maths, dates, random numbers, file paths and JSON: the standard library already has them.
- Splitting your own project into several files and importing between them.
- Using installed packages such as NumPy or pandas.
When not to use it
from module import *: it hides where names come from and can overwrite your own names.- Importing a large package for one tiny thing you can write in two lines.
Example code
# import loads a module from Python's standard library.
import math
import random
from datetime import date
from collections import Counter as Tally
print(math.sqrt(16))
print(math.pi)
print(math.floor(3.7), math.ceil(3.2))
# A fixed seed makes the "random" numbers repeat on every run.
random.seed(1)
print(random.randint(1, 6))
print(random.choice(["rock", "paper", "scissors"]))
print(date(2026, 1, 1).strftime("%A"))
# "as" gives an imported name a shorter or clearer alias.
print(Tally("banana").most_common(1))
Result of running the code
4.0
3.141592653589793
3 4
2
scissors
Thursday
[('a', 3)]
Code explanation
import math keeps the module name as a prefix, from datetime import date brings one name in directly, and as gives an import a shorter alias. random.seed makes the random results repeat.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Naming your file like a module
A file called random.py in your folder is imported instead of Python's random module.
# file name: random.py
import random
random.randint(1, 6) # AttributeError
# rename your file to dice.py
import random
random.randint(1, 6)
Forgetting the module prefix
After import math, the function is math.sqrt, not sqrt.
import math
print(sqrt(16))
import math
print(math.sqrt(16))
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges