Learning LibraryCore Coding LibraryTeens

Imports in Python: modules, packages, and from-import

No real program is written from scratch. import is how you pull in code — our game entities, Python's random, thousands of libraries — and build on top of it.

The big idea

import loads a module or package so its names are available in your file; from X import Y pulls one specific name out of it.

See it in code

1The basics

There are two ways to import, and this is the first: load a whole module and keep its name as a prefix. import math gives you everything in math, reached with a math. in front:

python
import math

print(math.sqrt(144))
Run it — the prefix says where the tool came from:
12.0

import math loads the module; math.sqrt reaches in for one function. The math. prefix makes the source obvious at a glance.

2A step further

The second way pulls one name straight out of a module, so you can drop the prefix. from math import sqrt grabs just sqrt — now you call it directly:

python
from math import sqrt

print(sqrt(144))
Run it — same result, no prefix needed:
12.0

Same tool, two styles: math.sqrt keeps the prefix, from math import sqrt drops it. Use the prefix for clarity, the plain name for things you call constantly.

3In our world

Now both flavors in one program. import random loads the whole random module — we reach in with random.randint(...), just like math.sqrt. from game import Stage, Enemy pulls two names straight out so we write Enemy(...) directly. We seed random first so the layout is identical every run:

python
import random
from game import Stage, Enemy

screen = Stage.create()
Stage.clear(screen)

random.seed(7)
for i in range(6):
    x = random.randint(40, 440)
    Enemy(x, 90).draw(screen)
Run it — a scattered wave, positioned by the random module:
Six purple enemy sprites scattered at random horizontal positions near the top of a dark play area.

import random keeps the module's name as a prefix (random.randint), which makes it obvious where the tool came from. from game import Enemy drops the prefix for names you use constantly. Both are just ways to reach borrowed code.

The same idea, everywhere

This is universal: a data script imports csv, a web backend imports its framework, a biotech tool imports a sequence parser. Packages bundle related modules; from ... import ... cherry-picks what you need. The skill is the same whether the code is ours, Python's, or a stranger's.

Try it yourself

Change random.seed(7) to random.seed(1) and watch the whole layout shift — a fixed seed makes randomness reproducible. Then swap import random for from random import randint and simplify the call to plain randint(40, 440).

The common mistake

Shadowing a real module. Naming your own file random.py means import random finds your file instead of Python's module, and everything breaks in confusing ways. Never name a file after a module you import.

What it unlocks

Imports open the whole ecosystem: the random module for unpredictability, the math module for trig and roots, and every domain package across these courses.

Want the simpler version? Read the Kids version →