Importing Modules
About 15 minutes
Why this matters
Python ships with a huge standard library of ready-made tools: maths, dates, files, JSON, random numbers and more. You use them with import. The same idea lets you split your own code across files.
The idea
import math # use as math.sqrt(16)
from math import sqrt, pi # use as sqrt(16) and pi
import statistics as stats # a shorter name: stats.mean([1, 2, 3])
import math is the safest: you always see where a name comes from. Avoid from math import *, which dumps every name into your file.
To learn what a module offers, use help(math.ceil) or dir(math).
Every file has a built-in name, __name__. When you run the file it is "__main__"; when another file imports it, it is the module’s name. That gives the standard pattern:
def main():
print("running")
if __name__ == "__main__": # only when run directly, not when imported
main()
Try it
In the editor, import math and print math.pi, math.sqrt(2) and math.floor(-2.5). Then add print(__name__) and see what it shows.
Your turn
Write:
circle_area(radius): the area, rounded to 2 decimals (circle_area(2)is12.57). Usemath.pi.pages_needed(items, per_page): how many pages are needed to showitemsitems withper_pageon each page. 23 items at 10 per page need 3 pages. Usemath.ceil.summarize(numbers): a dictionary{"mean": ..., "median": ...}using thestatisticsmodule, with the mean rounded to 2 decimals.main()that printsArea: 12.57(using yourcircle_area(2)), called only under theif __name__ == "__main__":guard.
Recap
import modulethenmodule.name;from module import namefor a few names.- The standard library is big: look before you write your own.
if __name__ == "__main__":runs code only when the file is run directly.
Common mistakes
- Naming your own file like a module (
random.py,math.py). Python imports yours instead and things break:AttributeError: module 'random' has no attribute 'randint'. - Forgetting the module name:
sqrt(4)afterimport mathis aNameError. - Putting code at the top level of a file you want to import, which then runs on import.