Modules packages and imports
Chapter 63 of 132 · Level 4 · Professional Python & OOP · Python demonstrations with recorded outputs. Learn through reading, predicting and testing real Python code.
Course disclaimer · independent educational content
01 · Why this concept matters
Modules group related definitions. A package groups modules under an importable namespace. Knowing where Python searches for imports helps you avoid accidentally naming your file math.py.
Learning goal: Modules packages and imports. Start by reading what the program does, then inspect its exact output. Where an example illustrates only a prerequisite, study the named advanced feature separately before using it in production.
02 · The important subtopics
Study these connected ideas as you work through the demonstration:
- Package layouts and __init__
- Absolute and relative imports
- Import side effects and cycles
- python -m entry points
03 · Pinpoint: read the code line by line
- Line 1
import mathDefine or import. Notice what values enter this statement and which names it affects. - Line 2
from pathlib import PathDefine or import. Notice what values enter this statement and which names it affects. - Line 3
print(math.isqrt(81))Display a result. Notice what values enter this statement and which names it affects. - Line 4
print(Path('lesson.py').suffix)Display a result. Notice what values enter this statement and which names it affects.
Find the literal input values and arguments, follow each operation in execution order, and verify the result below. This habit turns a code listing into a testable explanation.
04 · Runnable Python example and verified output
Modules packages and imports: working demonstration
Python · runnableimport math
from pathlib import Path
print(math.isqrt(81))
print(Path('lesson.py').suffix)
9 .py
Why it works: Modules group related definitions. A package groups modules under an importable namespace. Knowing where Python searches for imports helps you avoid accidentally naming your file math.py.
Worked example 2 · Modules Packages And Imports in practice
Python · executed and checkedfrom importlib import import_module
math = import_module('math')
print('Square root:', math.isqrt(81))
Square root: 9
Pinpoint learning: This practical modules packages and imports demonstration uses a small input you can inspect. Run it exactly as shown, compare the displayed result, then change one value and predict how the result will change.
Understand the code line by line
1: from importlib import import_module— Load a standard-library feature needed by the example.2: math = import_module('math')— Store or update the value on the left using the expression on the right.3: print('Square root:', math.isqrt(81))— Display the value so you can compare it with the verified output.
05 · Independent practice and answer guidance
Exercise A · Adapt the code
Design a package with a helpers module and a separate main script; identify each import path.
Exercise B · Predict
Before pressing Run, predict the first displayed line. Next, change one literal value and record the new observed output. Explain the difference in one clear sentence.
Exercise C · Debug
Make a temporary invalid change, read the exception or failed assertion if one occurs, restore the working code, and explain the repair.
Worked explanation for the original example
Modules group related definitions. A package groups modules under an importable namespace. Knowing where Python searches for imports helps you avoid accidentally naming your file math.py.
9 .py
06 · Build, verify and advance
Real-world task: Split one large program into a reusable package.
Run the original code; explain the important lines; change one input and verify the new outcome; use a test or recorded result to justify your conclusion.
