- Courses
- 💻Programming Languages
- 🐍Python
- Functions and Modular Programming
- Creating Your Own Modules
On this page
Creating Your Own Modules
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
Designing a Module File
A module is created simply by writing Python code in a .py file. The goal is to group related functionality into a single, coherent unit.
Example module file:
# math_utils.py
def add(a, b):
return a + b
def subtract(a, b):
return a - b
This file defines the math_utils module, which can be imported into any other script.
Importing Your Module
To use the module in another file:
import math_utils
result = math_utils.add(3, 4)
This form keeps the namespace clear and avoids naming conflicts.
Importing Specific Definitions
from math_utils import add
Now you can call:
add(3, 4)
This is concise but requires careful naming to avoid collisions.
Creating Aliases
Aliases improve readability when module names are long:
import math_utils as mu
mu.add(5, 6)
Aliases are widely used in scientific and data‑analysis codebases.
Organizing Multiple Modules
As a project grows, you will naturally divide functionality across several modules.
Example structure:
project/
math_utils.py
string_utils.py
main.py
In main.py:
import math_utils
import string_utils
Each module focuses on a specific responsibility, improving maintainability.
Creating a Package
A package is a directory that contains multiple modules. It must include an __init__.py file.
Example:
my_app/
__init__.py
math_utils.py
string_utils.py
Importing from a package:
from my_app import math_utils
Packages allow you to build structured, scalable applications.
Controlling What a Module Exports
Inside a module, you may define an __all__ list to specify which names are exported when using wildcard imports.
__all__ = ["add", "subtract"]
Wildcard imports are discouraged, but understanding __all__ is important for writing intentional, well‑structured modules.
Using the __name__ Guard
Every module has a built‑in attribute named __name__. When the file is executed directly, __name__ equals "__main__". When imported, it equals the module’s name.
This enables the standard pattern:
def main():
print("Running module")
if __name__ == "__main__":
main()
This ensures that test or demonstration code runs only when the module is executed directly, not when imported.
Best Practices for Creating Modules
- Use clear, descriptive filenames
- Group related functionality together
- Keep modules cohesive and focused
- Avoid circular imports
- Place imports at the top of each file
- Document all public functions and classes
- Use packages for larger projects
- Prefer explicit imports over wildcard imports
These practices ensure clarity, maintainability, and long‑term scalability.
Summary
This lesson showed how to create your own modules, import them, organize multiple files, structure packages, control exports, and apply best practices for modular programming. These skills form the foundation for building clean, reusable, and professional Python codebases.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Modules & Imports
Next
Guided Exercise: Build a Modular Utility Toolkit
-
Designing a Module File
-
Importing Your Module
- Importing Specific Definitions
- Creating Aliases
-
Organizing Multiple Modules
-
Creating a Package
-
Controlling What a Module Exports
-
Using the
__name__Guard -
Best Practices for Creating Modules
-
Summary
