What Is a Module in Python?
A module in Python is a file that contains code you can reuse.
That file can contain:
- functions
- variables
- classes
- import statements
- code that runs when the file is imported or executed
You use a module by importing it into another Python file.
A simple way to think about it is this:
- one file stores useful code
- another file uses that code
This page explains what a module is and why it matters. If you want the full mechanics of import, see how import works in Python.
Simple definition
A Python module is usually a single .py file.
For example, if you have a file named helpers.py, that file can be a module. Another file can import it and use code from it.
Here is the basic idea:
helpers.pycontains reusable codemain.pyimportshelpers.pymain.pyuses whathelpers.pyprovides
In beginner terms, this is the most important idea:
A module is a Python file that helps you organize and reuse code.
Why modules are useful
Modules make Python programs easier to work with.
They help because:
- They organize code into smaller files
- They reduce repetition because you can reuse the same code
- They improve readability by separating different parts of a program
- They make projects easier to maintain as your code grows
Without modules, you may end up putting everything into one large file. That quickly becomes hard to read and update.
What can be inside a module
A module can contain many kinds of Python code.
Common examples include:
- Functions such as
greet()orcalculate_total() - Variables such as
TAX_RATEorapp_name - Classes such as
UserorCar - Imports and supporting code used by the module
Example module:
# helpers.py
app_name = "My Program"
def greet(name):
return f"Hello, {name}!"
This module contains:
- one variable:
app_name - one function:
greet()
A module does not have to contain only functions. Beginners often assume that, but modules can store many kinds of code.
Module vs package
A module is usually one Python file.
A package is a folder that groups related modules together.
So the difference is:
- module = one
.pyfile - package = one folder containing related Python code
This page is only defining the term module. For the full distinction, see what is a package in Python.
Built-in modules and your own modules
Some modules come with Python already installed. These are often called built-in or standard library modules.
Examples include:
mathrandomos
For example:
import math
print(math.sqrt(9))
Expected output:
3.0
You can learn more from this Python math module overview.
You can also create your own module by making a .py file.
Beginners often start by importing built-in modules first. Later, when programs get larger, custom modules become very useful for organizing your own code.
Basic example idea
Here is a minimal example showing one custom module and one file that uses it.
File 1: helpers.py
def greet(name):
return f"Hello, {name}!"
File 2: main.py
import helpers
print(helpers.greet("Ava"))
Expected output:
Hello, Ava!
What is happening here:
helpers.pyis the modulemain.pyimports that modulemain.pyuses thegreet()function from the module
This is one of the simplest examples of a Python module.
If you want to go deeper into the import statement, read Python modules explained or how import works in Python.
Important beginner note
A few details are easy to miss when you are new to modules.
- A module is not the same as a package
- Importing a module runs top-level code in that file
- The file name matters when importing
- You should avoid naming your file the same as a standard library module
For example, if you create a file named random.py, it can conflict with Python’s built-in random module.
That can cause confusing import problems later.
Common mistakes
Beginners often run into these problems:
- Confusing a module with a package
A module is usually one file. A package is a folder of related modules. - Thinking a module must be built in
Built-in modules are modules, but your own.pyfiles can be modules too. - Assuming a module can only contain functions
Modules can also contain variables, classes, imports, and other code. - Naming a file after a standard library module
Avoid names likerandom.py,json.py, ormath.py. These can shadow the real module and cause import errors.
If Python cannot find the module you expect, see how to fix ModuleNotFoundError: No module named 'x'.
FAQ
Is a module the same as a Python file?
Usually, yes.
In beginner terms, a module is commonly a .py file that can be imported.
Can I make my own module?
Yes.
Create a Python file and import it from another file.
What is the difference between a module and a package?
A module is one file. A package is a folder that groups modules together.
Do built-in modules count as modules?
Yes.
Modules like math, random, and os are Python modules.