π Python Creating & Importing Modules β Reuse and Organize Your Code
π§² Introduction β Why Create and Import Modules?
As your code grows, maintaining everything in a single Python file becomes difficult. To stay organized and reusable, Python allows you to split your code into modules.
A module in Python is simply a .py file that contains functions, variables, or classes. These modules can be imported into other scripts, making your programs modular, clean, and easier to manage.
π― In this guide, you’ll learn:
- How to create a custom Python module
- How to import and use modules in other files
- The difference between
import,from, andas - Module execution via
__name__ == "__main__" - Best practices for module development
π§Ύ What is a Python Module?
A Python module is any file ending in .py that contains reusable codeβlike functions, constants, or classes.
β Example: Creating and Importing a Custom Module
Step 1: Create math_utils.py
# math_utils.py
def add(a, b):
return a + b
def multiply(a, b):
return a * b
Step 2: Create main.py and Import It
# main.py
import math_utils
print(math_utils.add(3, 5)) # Output: 8
print(math_utils.multiply(4, 6)) # Output: 24
π Import Techniques
| Syntax | Description |
|---|---|
import module | Imports the full module |
import module as alias | Uses a shorter alias for module |
from module import function | Imports specific function from module |
from module import * | Imports all members (not recommended) |
π Using __name__ == "__main__" in Modules
To make your module run as a script and also be importable, use:
# math_utils.py
def add(a, b):
return a + b
if __name__ == "__main__":
print(add(2, 3)) # Only runs when the file is executed directly
π Organizing Modules in a Directory
You can store your modules in folders and import them as packages:
my_project/
βββ utils/
β βββ __init__.py
β βββ math_utils.py
βββ main.py
Then import like:
from utils import math_utils
π§ Best Practices for Module Creation
- β
Use lowercase names for modules (
math_utils.py) - β Group related functions/classes together
- β
Use
__all__in__init__.pyto control wildcard imports - β Include docstrings in your module and functions
- β Keep modules focused on a single responsibility
π Summary β Recap & Next Steps
Creating and importing modules in Python helps you write clean, reusable, and organized code. You can split functionality into logical units and reuse them across projects using the import system.
π Key Takeaways:
- β
A Python module is a
.pyfile that can be imported into another script. - β
Use
import,from, andasto control how you bring in functionality. - β
The
__name__ == "__main__"block makes modules reusable and executable. - β
Use folders +
__init__.pyto structure larger modules into packages.
βοΈ Real-World Relevance:
Modules are used in every real Python applicationβfrom simple scripts to massive web apps and APIs. They enable reusability, reduce code duplication, and allow better team collaboration.
β FAQ Section β Python Creating & Importing Modules
β What is a module in Python?
β
A module is any Python .py file that contains reusable code such as functions, classes, or variables.
β How do I import a module?
β
Use import module_name or from module_name import function_name.
β What does __name__ == "__main__" do?
β It ensures certain code only runs when the module is executed directly, not when imported.
β Can I import a module from a subfolder?
β
Yes. Ensure the folder has an __init__.py file and use dot notation to import:
from folder.module import name
β Should I always use from module import *?
β No. It can cause namespace pollution. Prefer importing specific functions or using aliases.
Share Now :
