Lesson 05
Reloading Python Modules with importlib.reload()
Understand when reload is needed using a portfolio and Netflix analogy — importlib.reload() picks up file changes while a program is still running.
One relatable example beats memorizing syntax. Python caches imported modules —
importlib.reload()refreshes them when you edit the source file.
Use the playground and quiz at the bottom to see before/after data change.
What you will learn
- Why Python caches imported modules in memory
- The Netflix analogy — downloaded copy vs fresh upload
- A portfolio shares walkthrough (before edit vs after reload)
- The only syntax you need:
importlib.reload(module) - When reload matters — and when it does not
The reload flow
Read it as: Import → Edit file → Reload → Use fresh data.
Why does this problem happen?
Python loads a module only once. After that, it keeps the module in memory.
Think of Netflix:
You downloaded Episode 1. Even if the studio uploads a new version, your TV still plays the downloaded copy until you refresh.
Exactly the same idea with Python modules:
If you:
import portfolio- Call
portfolio.sharesinfo() - Edit
portfolio.pyin your editor - Call
portfolio.sharesinfo()again
…you still get the old values. Python is using the cached module already in memory.
Key insight: Editing the file on disk ≠ updating what Python remembers in RAM.
Portfolio example (before vs after)
Imagine portfolio.py stores stock sector values.
Program still remembers the old data — even after you save changes to the file.
After importlib.reload(portfolio), the next call picks up the updated values.
Module — portfolio.py (version 1):
def sharesinfo():
d = {"Tech": 19, "Pharma": 11, "Auto": 1, "Finance": 0}
return dViewer program:
import portfolio
import importlib
def disp_shares(d):
print("-" * 50)
print("\tShare Name\tValue")
print("-" * 50)
for m, p in d.items():
print("\t{}\t\t:{}".format(m, p))
print("-" * 50)
# First load
d = portfolio.sharesinfo()
disp_shares(d)
# User edits portfolio.py — new values:
# {"Tech": 25, "Pharma": 15, "Auto": 3, "Finance": 20}
importlib.reload(portfolio)
d = portfolio.sharesinfo()
disp_shares(d)The only syntax to remember
Reload works on an already imported module object, not a filename string.
import portfolio
import importlib
importlib.reload(portfolio) # pass the module object
d = portfolio.sharesinfo() # use fresh data| Piece | Role |
|---|---|
importlib |
Standard library module for import utilities |
reload() |
Re-executes the module's source file |
portfolio |
The module object you already imported (not "portfolio") |
When do you actually use reload?
| Situation | Reload? |
|---|---|
| Jupyter Notebook | Yes |
| Python REPL | Yes |
| Normal script (run again) | No — just re-run the program |
| Flask/Django development | Usually auto-reloader handles it |
Rules and caveats
- Import first —
reload()needs an already-imported module object - Pass the module object —
importlib.reload(portfolio), not"portfolio" - Existing references — objects created before reload may still point to old code
- Side effects run again — module-level code executes a second time on reload
Full workflow (conceptual)
import portfolio
import importlib
d = portfolio.sharesinfo()
disp_shares(d)
# ... time to edit portfolio.py externally ...
importlib.reload(portfolio)
d = portfolio.sharesinfo()
disp_shares(d)Interview cheat sheet
| Question | Answer |
|---|---|
| Why reload? | Module is cached in memory |
| Function used | importlib.reload() |
| Argument | Module object (not string) |
| Most common use | Jupyter / interactive development |
One-line memory rule: Import → Edit file → Reload → Use fresh data.
Key takeaways
- Python caches modules after the first import
- Editing a .py file does not update the in-memory copy
importlib.reload(module)re-reads the source file- Pass the module object, not a string name
- Most useful in Jupyter and REPL sessions
- Next: combine everything in a multi-module calculator project
Next lesson
The final lesson wires menu, operations, and main program across separate files:
from calc_menu import show_calc_menu
from calcops import addop, subop, mulopThat is where import syntax meets a complete application structure.
Loading playground…
Check your understanding
Module reload quiz
Which function reloads a previously imported module?
1 / 10