Python Tutorial 0/52 lessons ~6 min read Lesson 29

    Modules & Imports

    A module is a .py file.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    A module is a .py file. A package is a directory with __init__.py. import caches modules in sys.modules.

    Understanding the topic

    Core concepts to understand:

    • import math — namespace import.
    • from math import sqrt — specific import.
    • Absolute vs relative imports.
    • if __name__ == '__main__': for scripts.

    Syntax reference

    Visual flow / code:

    python
    # project/utils/math.py
    def square(x): return x * x
    # project/main.py
    from utils.math import square
    from utils import math as M
    print(square(5))
    print(M.square(6))
    if __name__ == "__main__":
    print("Run as script")

    Execution workflow

    1Modules & Imports Workflow
    1 / 4

    import math

    namespace import.

    Real-world use

    Modern packaging uses src/ layout + pyproject.toml. Avoid from x import * — explicit imports beat magic.

    Best practices

    • Prefer absolute imports.
    • Never import * in production code.
    • One responsibility per module.

    Common mistakes

    • Circular imports — split shared types into a separate module.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestion__name__ == '__main__' — why?+

    Answer

    When a file is run as a script, Python sets its __name__ to '__main__'; when it is imported, __name__ is the module's import name. The guard lets a file hold reusable definitions and also run entry-point code only when executed directly, so importing it has no side effects such as starting a server or running tests.

    Follow-up

    What changes when you run it with python -m package.module?
    2BeginnerQuestionAbsolute vs relative import?+

    Answer

    An absolute import names the full path from a top-level package, such as from app.utils import helper. A relative import uses dots relative to the current package, such as from .utils import helper, and only works inside a package, not in a script run directly. PEP 8 prefers absolute imports for clarity; relative ones are fine for tightly coupled modules in the same package.

    Follow-up

    Why does a relative import fail with 'attempted relative import with no known parent package'?
    3IntermediateQuestionWhat's sys.modules?+

    Answer

    sys.modules is a dict mapping fully qualified module names to already-imported module objects. The import system checks it first, so a second import x returns the cached module without re-executing it. You can delete an entry to force a reimport or insert a fake module for testing, but both need care.

    Follow-up

    How do circular imports interact with sys.modules?

    Summary

    In summary: File = module; dir = package. Explicit imports always.

    Ready to mark this lesson complete?Track your journey across the entire course.