Importing modules
🌍 In real life. A carpenter doesn't forge a new hammer for every job. The tools sit in labelled boxes in the workshop, and she brings the box she needs to her bench: the drill box, the measuring box. Once a box is on the bench, everything in it is in reach.
Python's ready-made tools come in boxes too, called modules, and import brings a box to your desk.
The idea in one sentence: a module is a file of ready-made code; import math puts the name math on your desk, and you reach its tools with a dot, math.sqrt(16).
Its words:
- module: a
.pyfile of code you can use from other programs - standard library: the modules that come with Python:
math,random,json,csvand many more import math: brings the whole module, under the namemathfrom math import sqrt: brings just one name;import statistics as st: a shorter name
Step by step, for import math then math.sqrt(16):
- Python finds the
mathmodule and runs it once, making all its tools. - It puts one name on your desk:
math. math.sqrtlooks inside that module forsqrt, andmath.sqrt(16)gives 4.0.
import math
print(math.sqrt(16))
print(math.pi)
print(math.floor(2.7), math.ceil(2.1))
print(math.factorial(5))from math import sqrt, pi copies just those names, so you can write sqrt(25) without math. in front. import statistics as st gives the module a shorter name.
from math import sqrt, pi
import statistics as st
print(sqrt(25))
print(round(pi, 2))
print(st.mean([4, 8, 6]))import math only puts the name math on your desk. Writing floor(2.7) on its own is a NameError: either write math.floor, or import floor itself.
Why modules? Nobody has to rewrite square roots, random numbers or date maths: someone wrote and tested them once, and everyone imports them. A module is also run only once per program, however many times you import it, so importing is cheap after the first time.
Which form to use? import math keeps it clear where each name came from (math.floor). from math import floor is shorter but puts the name straight on your desk, where it could clash with one of your own.
Mistakes and edge cases.
- The name without the module. After
import math,floor(2.7)givesNameError: name 'floor' is not defined(above). - A misspelt module.
import MathgivesModuleNotFoundError: No module named 'Math': module names are case-sensitive. - A misspelt name.
from math import sqrootgives an ImportError: cannot import name 'sqroot'. - Naming your own file like a module. A file called
random.pynext to your program would be imported instead of Python'srandom.
Quiz
What does this print?
import math print(math.sqrt(49))What does this print?
from math import ceil, floor print(ceil(4.2), floor(4.8))After
import math, how do you use sqrt?- math.sqrt(9)
- sqrt(9)
- import.sqrt(9)
What does this print?
import statistics as st print(st.median([5, 1, 3]))Which line has a mistake?
import math radius = 3 area = pi * radius ** 2 print(area)
Coding tasks
Write
hypotenuse(a, b)that returns the long side of a right triangle: the square root of a² + b², usingmath.sqrt.Write
circle_area(r)that returns the area of a circle, π × r², usingmath.pi.Write
boxes_needed(items, per_box)that returns how many boxes you need: items ÷ per_box, rounded up withmath.ceil.