Skip to content

    Modules & Packages

    Python · Beginner

    Definition

    A module is a .py file; a package is a folder of modules. import loads them.

    Organizes code and lets you reuse libraries like numpy and pandas.

    Diagram

    main.pyutils.pypkg/__init__.py

    Flowchart

    Write functions in a fileimport moduleUse module.nameInstall third-party with pip

    Example: Using the math module

    import math
    print(math.sqrt(16), math.pi > 3)

    Output: 4.0 True

    Key points

    • if __name__ == '__main__' runs only when executed directly
    • Use virtual environments per project

    Interview questions

    What is __name__?

    '__main__' when the file runs directly, else the module name.

    More Python topics