Modules and Imports

Use code from elsewhere

Beginner 10 min

In this lesson

A module is a file of Python code you can pull into your program with import — so you don’t have to write everything yourself. Python ships with a huge standard library, and you can import your own files too.

Explain it like I’m 5

Importing is borrowing a ready-made toolbox. Instead of building a hammer from scratch, you say ‘give me the hammer’ and start using it.

What a module is and how to import it

A module is just a .py file containing functions, variables, and classes. import math loads the math module; you then use its contents with a dot: math.sqrt(16). The dot makes it clear where something came from, which keeps names tidy as your program grows.

Example
import math

print(math.sqrt(16))
print(math.floor(3.7))
Output
4.0
3
Reach into a module with the dot.

The forms of import

import math brings in the whole module (use math.pi). from math import sqrt brings in just one name (use sqrt directly). import datetime as dt imports under a shorter alias. Prefer plain import module or from module import name; avoid from module import *, which dumps everything and hides where names came from.

Example
import math
from random import randint

print(math.sqrt(144))
print(math.pi)
print("A dice roll between 1 and 6:", randint(1, 6))
Output
12.0
3.141592653589793
A dice roll between 1 and 6: 4
Two import styles in one program.

Import the math module and print the area of a circle with radius 5 (area = π × r²). Round it to 2 decimals.

import math
radius = 5
# area = pi * radius squared
area = ___
print(round(area, 2))

The standard library: batteries included

Python comes with dozens of ready-made modules: math for maths, random for randomness, datetime for dates and times, json for JSON data, os and pathlib for files and folders. Before writing something tricky from scratch, check whether the standard library already has it.

Example
from datetime import date

launch = date(2026, 1, 1)
today = date(2026, 6, 17)
days_open = (today - launch).days
print("Days since launch:", days_open)
Output
Days since launch: 167
Real date arithmetic, for free, from the standard library.

Importing your own files, and pip at a glance

If you have a file helpers.py next to your script, import helpers lets you use helpers.my_function(). Beyond the standard library, the wider community publishes packages you install with pip (covered later) — but the import statement is the same once they’re installed.

Explore any module with dir() and help()

You don’t have to memorise what a module offers — Python will tell you. dir(module) lists the names inside it, and help(thing) prints its documentation, including any docstrings.

These shine in the interactive REPL: import something unfamiliar, run dir() to see what’s available, then help() on whatever looks useful. It’s the fastest way to learn a new module without leaving Python.

Example
import math

print([name for name in dir(math) if name.startswith("s")])
help(math.sqrt)
Output
['sin', 'sinh', 'sqrt']
Help on built-in function sqrt in module math:

sqrt(x, /)
    Return the square root of x.
List what a module contains, then read the docs.

Common mistake: ModuleNotFoundError

Why it happens:

The name is misspelled, the module isn’t installed (for third-party packages), or your own file isn’t in the same folder / on the path.

How to fix it:

Check the spelling, confirm standard-library names, install third-party packages with pip, and keep your own modules alongside the script that imports them.

Common mistake: Naming your file the same as a module you import

Why it happens:

A file called random.py shadows the standard random module, so import random finds your file instead.

How to fix it:

Avoid naming your scripts after standard modules (math.py, json.py, random.py, etc.).

What is a module?

After import math, how do you call its sqrt function?

Mini exercise (easy)

Use the datetime module to print today’s date in YYYY-MM-DD form.

Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.

from datetime import date

today = None    # TODO: today’s date
print(today)

What to learn next

You learned what a module is, the forms of the import statement, the batteries-included standard library, how to import your own files, and a first look at pip packages. You imported datetime and used date.today() to get the current date.

As your programs grow, they will misbehave, and investigating calmly is a real skill. Debugging Basics gives you a process: reading tracebacks, inspecting values with print(), checking your assumptions, and isolating the problem. When you’re ready, continue to the next lesson.