Skip to content

Python Module

In one line

A module is a .py file containing Python code that can be imported and reused in other files.

In simple words

A Python module is simply a .py file. Any Python file can be imported by its name (without the .py extension) using the import statement. This lets you split code into focused, reusable files instead of one giant script.

For example, if you have a file math_utils.py with a function def add(a, b), another file can do from math_utils import add and use it. The module's name becomes a namespace, preventing name collisions between files.

Python's standard library is a collection of modules — os, sys, math, json, datetime, and many more. You import them the same way. Modules can also be organized into packages (folders of modules) for larger projects.

Example

python
# math_utils.py
def add(a, b):
    return a + b

PI = 3.14159

# main.py
from math_utils import add, PI

print(add(2, 3))  # 5
print(PI)         # 3.14159

math_utils.py is a module. main.py imports and uses its functions and variables.

Common confusions

  • Confused with: package

    The difference: A module is a single .py file. A package is a folder of modules with an __init__.py file. Every package contains modules, but a single module is not a package. Packages organize related modules into a hierarchy.

Related terms

Related Resources

Related Lessons

Learn the fundamentals

Deepen your understanding with structured lessons on this topic.

View lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary