📦 Creating Custom Modules in Python — Build Your Own Reusable Code
Introduction 🌟
A custom module is simply a Python file you create, containing functions, variables, or classes. Custom modules help make your code organized, reusable, and easier to maintain.
Note
💡 Any file ending with
.py is a Python module.1. Creating Your First Custom Module 🧱
Step 1: Create a file named mymath.py
mymath.py
def add(a, b):
return a + b
def sub(a, b):
return a - b
PI = 3.14159Step 2: Import & use it
use_mymath.py
import mymath
print(mymath.add(10, 20))
print(mymath.sub(50, 15))
print(mymath.PI)Note
✔ Python loads
mymath.py from the same folder.2. Using from ... import ... Style 🎯
from_import.py
from mymath import add, PI
print(add(5, 3))
print(PI)3. Adding Aliases with as 🔁
module_alias.py
import mymath as m
print(m.add(5, 2))4. Writing Custom Modules with Classes 🏗️
module_with_class.py
# file: shapes.py
class Circle:
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius * self.radiususe_shapes.py
from shapes import Circle
c = Circle(5)
print(c.area())5. Custom Modules with Helper Functions 🧰
utils.py
def greet(name):
return f"Hello {name}"
def is_even(n):
return n % 2 == 0use_utils.py
from utils import greet, is_even
print(greet("Sathish"))
print(is_even(12))6. Organizing Multiple Custom Modules into a Package 📁
A package is a folder containing multiple modules along with __init__.py.
package_structure
mypackage/
__init__.py
mathops.py
stringops.pyExample: mathops.py
mathops.py
def multiply(a, b):
return a * bExample: stringops.py
stringops.py
def uppercase(s):
return s.upper()Using the package
use_package.py
from mypackage.mathops import multiply
from mypackage.stringops import uppercase
print(multiply(4, 5))
print(uppercase("hello"))7. Config Modules — Storing Settings/Constants ⚙️
config.py
API_KEY = "12345ABC"
DB_NAME = "mydatabase"use_config.py
import config
print(config.API_KEY)
print(config.DB_NAME)8. Utility Modules — Reusable Common Code 🔧
helpers.py
def capitalize_words(text):
return " ".join(word.capitalize() for word in text.split())use_helpers.py
from helpers import capitalize_words
print(capitalize_words("python is awesome"))9. Custom Modules with Conditional Execution 🧠
conditional_module.py
# file: mymodule.py
def show():
print("This is my module")
if __name__ == "__main__":
print("Module executed directly")import_conditional.py
import mymodule
mymodule.show()Note
✔ Code under if __name__ == "__main__" runs only when the file is executed directly, not when imported.
10. Using dir() to Inspect Custom Modules 🔍
dir_custom_module.py
import mymath
print(dir(mymath))11. Module Search Path 🔎
sys_path.py
import sys
print(sys.path)✔ Python searches these folders when importing modules.
✔ You can add custom paths if needed.
12. Real-World Use Cases 🌍
🔹 Splitting large applications into smaller modules
split_app.py
# auth.py
def login(): pass
# db.py
def connect(): pass🔹 Reusable utilities across projects
utils_example.py
def slugify(title):
return title.lower().replace(" ", "-")🔹 Keeping configuration and secrets separate
secrets_config.py
SECRET_KEY = "xyz123"
DATABASE_URL = "mysql://user:pass@localhost/db"Conclusion 🎉
>>“Custom modules turn Python scripts into scalable, reusable components — the foundation of every large Python application.” ✨
You now fully understand Custom Modules in Python! Want the next topic? Try Packages, OOP (Classes & Objects), File Handling, Import System, or Virtual Environments. Just tell me! 😊