PY-101 ยท Modules and packages
import and from
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
import brings a module into your program, and you use its contents with a dot: import math, then math.sqrt(16). from math import sqrt imports one name so you can call sqrt directly, and as gives a module a shorter nickname. Modules let you reuse code instead of writing everything yourself.
- Import a whole module
- Import specific names with from
- Rename with as
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
import mathfrom statistics import mean, medianimport datetime as dtprint(math.sqrt(16), round(math.pi, 2))marks = [72, 90, 85, 64]print("Mean:", mean(marks), "Median:", median(marks))print(dt.date(2026, 12, 1).isoformat())
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
4.0 3.14
Mean: 77.75 Median: 78.5
2026-12-01 ๐ฏ Use from math import ceil to print how many boxes you need to pack 53 items, 10 per box (answer 6).
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
from math import ceil; print(ceil(items / per_box))
- The problemYou need a square root, an average and today's date. Writing those yourself would take an afternoon; Python already ships them, one import away.
- The Python idea
import moduleimport brings a module into your program, and you use its contents with a dot: import math, then math.sqrt(16). - In real lifeA typical data script starts with import pandas as pd, import numpy as np and import matplotlib.pyplot as plt.
Start with a question
You need a square root, an average and today's date. Writing those yourself would take an afternoon; Python already ships them, one import away.
Real programs are built from modules: Python's own, other people's from PyPI, and your own files.
Explain it like I'm 10
import is borrowing a toolbox from the shed. from ... import is borrowing just the hammer.
The technical explanation
import math then math.pi keeps the module name, so it is obvious where pi came from.
from statistics import mean, median imports just those two names.
import datetime as dt lets you write dt.date instead of the full name. pandas is almost always import pandas as pd.
Syntax
import module
from module import name
import module as short Real-world scenario
A typical data script starts with import pandas as pd, import numpy as np and import matplotlib.pyplot as plt.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: The standard library tour โQuestions people ask
What is the difference between a module and a package in Python?
A module is one .py file. A package is a folder of modules (traditionally with an __init__.py) that you import as a group, such as email or json.
Where does Python look for modules?
In the folder of the running script, then the standard library, then installed packages. sys.path lists the exact folders.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What is the difference between a module and a package in Python?
A module is one .py file. A package is a folder of modules (traditionally with an __init__.py) that you import as a group, such as email or json.
beginner Where does Python look for modules?
In the folder of the running script, then the standard library, then installed packages. sys.path lists the exact folders.
intermediate After import math, how do you call sqrt?
math.sqrt(9). Use the module name and a dot.
intermediate After from math import pi, how do you use pi?
pi. The name is imported directly.
Quick revision
Remember
- Import a whole module
- Import specific names with from
- Rename with as
- Avoid
from module import *. It dumps every name into your program and you can no longer tell where something came from.
Syntax
import module
from module import name
import module as short Official documentation
- Modules (docs.python.org)