Three import styles
math and m print the same result because they are the same module object. Try print(sqrt) to see that a from-import gives you the function itself.
import loads another module and gives you a name for it. Python runs the module's file once, the first time it is imported, and keeps the result in sys.modules so that every later import is instant and shares the same module object.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Import sqrt from math under the name root, then print root(144).
Write a program that imports json, then prints "json" in sys.modules (import sys too) and the module's name json.__name__.
Write a file shapes.py that sets SIDES = 4 and prints loading shapes. (Create it from your program with Path("shapes.py").write_text(...), as in the second example.) Import it twice and show that the message appears only once.
Import sys. Print "antigravity" in sys.modules. Then import the module colorsys (do not import antigravity) and print the same check again. Explain the two answers.
Explain it without notes
What exactly happens when Python executes import greeter for the first time?
What is sys.modules, and what does it explain?
What is the difference between import x and from x import name?
Why is from module import * discouraged?
Expected output
4.0 5.0 6.0
pi is about 3.1416
same module object: True
math in sys.modules: True
type of sys.modules: dict
module math