14 / 14
Guide Book · Chapter 14

Modules & imports

How import finds and runs code, sys.modules, packages, `if __name__ == "__main__"`, and a tour of the standard library.

Import forms

python
import math                       # the module object, use math.sqrt
from math import sqrt, pi         # specific names
from collections import Counter as C   # rename
import json as j

print(math.sqrt(16), sqrt(9), pi, C("aab"), j.dumps({"ok": True}))

⚠️ Gotcha: avoid from module import *. It dumps unknown names into your namespace and makes it unclear where anything came from.

python
import sys, math

print("math" in sys.modules, type(math), math.__name__)
print(len(sys.modules), "modules loaded so far")
print(sys.path[:3])

Modules are objects

python
import math

print([name for name in dir(math) if not name.startswith("_")][:10])
print(getattr(math, "tau"))

if __name__ == "__main__":

Every module has a __name__. When a file is run directly, its __name__ is "__main__"; when it's imported, it's the module's name. This guard lets a file be both a runnable script and an importable library.

python
def main():
    print("running as a script")

if __name__ == "__main__":
    main()
else:
    print("imported, so main() doesn't run")

Writing and importing your own module

In ThongLearn everything runs from one editor, but you can write a real module file into the in-memory filesystem and import it:

python
import sys, importlib

with open("greetings.py", "w") as f:
    f.write('print("module code runs once")\ndef hello(name):\n    return f"hello {name}"\n')

sys.path.insert(0, ".")
import greetings
import greetings            # cached: the print doesn't happen again
print(greetings.hello("Ada"), greetings.__file__)
importlib.reload(greetings) # forces a re-run

A standard library tour

ModuleUse it for
collectionsCounter, defaultdict, deque, namedtuple
itertools / functoolsiteration tools, cache, partial, reduce
datetime / timedates, times, durations
json / csvreading and writing data formats
reregular expressions
pathlib / osfiles and paths
random / statistics / mathnumbers
dataclasses / typing / enummodeling data
python
import re, json, statistics
from datetime import date, timedelta
from enum import Enum

print(re.findall(r"\d+", "order 12 of 345"))
print(json.loads('{"a": [1, 2]}')["a"])
print(statistics.mean([2, 4, 9]), statistics.median([2, 4, 9]))
print(date(2026, 1, 1) + timedelta(days=45))

class Size(Enum):
    S = 1
    M = 2
print(Size.M, Size(1).name)

🧭 Scenario: You split a growing script into models.py, storage.py and main.py. Keep the side effects (printing, starting things) inside main() behind the __name__ guard, so importing models from a test file never accidentally runs your whole program.

main.py
Loading editor…
No output yet
Press Run or ⌘↵ to compile and run your code.

Search

Jump to a lesson or a past run