Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate25 min
Reloading Python modules hero — Module 14 Lesson 5 importlib reload flow

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

Import edit reload use four step module reload flow diagram

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.

Netflix analogy downloaded episode stays until refresh like Python module cache

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:

portfolio.py file vs RAM cache first import editing file does not update memory

If you:

  1. import portfolio
  2. Call portfolio.sharesinfo()
  3. Edit portfolio.py in your editor
  4. 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.

portfolio.py version 1 before edit Tech 19 Auto 1

Program still remembers the old data — even after you save changes to the file.

portfolio.py version 2 after reload Tech 25 Auto 3 Finance 20

After importlib.reload(portfolio), the next call picks up the updated values.

Module — portfolio.py (version 1):

python
def sharesinfo():
    d = {"Tech": 19, "Pharma": 11, "Auto": 1, "Finance": 0}
    return d

Viewer program:

python
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

import portfolio importlib.reload portfolio module object syntax

Reload works on an already imported module object, not a filename string.

python
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

  1. Import first — reload() needs an already-imported module object
  2. Pass the module object — importlib.reload(portfolio), not "portfolio"
  3. Existing references — objects created before reload may still point to old code
  4. Side effects run again — module-level code executes a second time on reload

Full workflow (conceptual)

python
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

importlib reload interview cheat sheet cached module Jupyter REPL
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:

python
from calc_menu import show_calc_menu
from calcops import addop, subop, mulop

That 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