On this page

Modules & Imports

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

What a Module Is​

A module is simply a Python file (.py) that contains definitions—functions, classes, variables—that you can reuse in other programs. Modules allow you to break large programs into smaller, logical components. This improves readability, maintainability, and collaboration.

Python’s module system is one of its most important features for building scalable software.

Why Modules Matter​

Modules enable you to:

Modular programming is a core skill for any professional Python developer.

Creating Your Own Module​

Any Python file can act as a module. For example, create a file named math_utils.py:


def add(a, b):

    return a + b



def multiply(a, b):

    return a * b

This file now defines a module named math_utils.

To use it in another file:


import math_utils



result = math_utils.add(3, 4)

The module name matches the filename (without .py).

Importing a Module​

The simplest import form is:


import module_name

After importing, you access its contents using dot notation:


module_name.function_name()

This keeps namespaces clear and avoids naming conflicts.

Importing Specific Names​

You may import only the parts you need:


from math_utils import add

Now you can call:


add(3, 4)

This makes code shorter but requires careful naming to avoid collisions.

Importing Multiple Names​


from math_utils import add, multiply

Renaming Imports​

Use as to create an alias:


import math_utils as mu

mu.add(3, 4)

Aliases improve readability when module names are long.

Importing Standard Library Modules​

Python includes a rich standard library—modules that come bundled with Python and provide essential functionality.

Examples:


import math

import random

import datetime

Using them:


math.sqrt(16)

random.choice([1, 2, 3])

datetime.date.today()

The standard library is authoritative, well‑documented, and optimized.

Importing Third‑Party Modules​

You can install external modules using package managers such as pip. Once installed, they are imported like any other module:


import requests

Third‑party modules extend Python’s capabilities but should be chosen carefully based on documentation quality and maintenance.

The Module Search Path​

When you import a module, Python searches for it in a specific order:

  1. The current working directory
  2. Installed packages and standard library
  3. Paths listed in sys.path

Understanding this helps diagnose import errors and structure projects correctly.

The __name__ == "__main__" Pattern​

Modules often contain both definitions and executable code. To prevent certain code from running when the module is imported, Python uses a special variable: __name__.


def main():

    print("Running module")



if __name__ == "__main__":

    main()

When the file is executed directly, __name__ equals "__main__".

When imported, it equals the module’s name.

This pattern is essential for writing reusable modules that also support direct execution.

Organizing Code into Packages​

A package is a directory containing multiple modules. A simple package structure:


my_app/

    __init__.py

    utils.py

    models.py

The presence of __init__.py tells Python that the directory is a package.

Importing from a package:


from my_app import utils

from my_app.models import User

Packages allow you to build large, well‑structured applications.

Best Practices for Modules and Imports​

To write clean, maintainable modular code:

These practices ensure clarity and long‑term maintainability.

Summary​

In this lesson, you learned how Python modules and imports work, how to create your own modules, how to use the standard library, how to structure packages, and how to follow best practices for modular programming. These skills are essential for building real‑world Python applications that are organized, scalable, and easy to maintain.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Lambda Functions

Next

Creating Your Own Modules