A star import silently replaces the built-in sum
The file mathy.py is written first (def makes a function, Phase 5). It defines its own sum. After from mathy import *, the name sum in this file points to it. The real built-in is still reachable through the builtins module.
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
before: 6
after star import: my own sum
builtin still fine: 6