Modules: import, from, search path

A module is a file of names. import loads it once. from copies names. The search path is why ‘it works on my USB’ fails on the lab PC. reload is an interactive habit, not a production one.

Application blueprint

A program is folders, config and a main door.

You will be able to

  • Treat a module as a .py file of names that import can load
  • Keep import quiet — no receipt at load time
  • Know the search path is why a USB script fails on the lab PC (reload is interactive only)

import loads a file of names once

fees.py holds late_fee and maybe CONFIG. import fees loads that file and binds the module name. from fees import late_fee copies the function into your file’s names. Either way, running the import must not print a hostel receipt. If it does, every test and every dashboard that imports fees prints a receipt. The door stays in the next lesson; the habit starts here: functions at the top, silence at load.

Python searches a list of folders (sys.path on a laptop) for fees.py. “It works on my USB” fails when the lab PC’s path does not include that stick. Put the project in one folder, run from that folder, or install the package. Do not copy fees.py into site-packages by hand.

Laptop: import is quiet; path is a list of folders

# fees.py
def late_fee(days):
    if days <= 0:
        return 0
    return 25

# app.py
import fees
from fees import late_fee
print(late_fee(4))
# importlib.reload(fees)  # REPL only, not a nightly job

from and reload are easy to misuse

from fees import late_fee is convenient. from fees import * is how names collide — avoid it until you meet __all__. importlib.reload is an interactive habit after you edited a file in a long REPL session. Production jobs start a new process; they do not reload. This sandbox cannot import, so you practise two functions in one file-shaped program: the “module” stays quiet until the “app” prints.

Words that matter

Module
A Python file whose names can be imported.
import
Load the file once; bind the module (or copy names with from).
Search path
Folders Python walks to find fees.py — why USB ≠ lab PC.
reload
Interactive re-load after an edit — not a production pattern.

Common mistakes

Avoid: print at the bottom of every helper file so “you can see it working”.

Do this: return from helpers; the importer stays silent.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — helpers stay quiet

Defining is not printing. Only loaded appears.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — the app file is the one that prints

Same function, now called. Fee 25.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

On a full Python install — your own module

Save late_fee in fees.py and import it from app.py. Import must stay silent. Search path is the project folder.

No pip — files you wrote.

Quiet import, one public function.

Real library code (not run in this browser sandbox)

# fees.py
def late_fee(days):
    if days <= 0:
        return 0
    return 25

# app.py
from fees import late_fee

print(late_fee(4))

Example program — Two callers, one module-shaped function

Kiosk and batch both call late_fee. Neither copies the 25.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Silent rule, loud caller

Define gst(amount) as amount * 118 / 100. Print only gst(100) so 118 appears.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. import should…
2. reload belongs in…

Progress is stored in a browser cookie on this device.