π¦ Python Modules β Organizing Code into Reusable Files
Introduction π
A module in Python is simply a file that contains Python code β functions, variables, classes, or executable statements. Modules help make code organized, reusable, maintainable, and scalable.
Note
π‘ Any .py file is a module.
Example: math.py is a module.
Example: math.py is a module.
1. Creating Your Own Module π§±
Step 1: Create a Python file
Create a file named:
Code Snippet
mymath.pyStep 2: Add functions
mymath.py
def add(a, b):
return a + b
def sub(a, b):
return a - bStep 3: Import and use the module
use_module.py
import mymath
print(mymath.add(10, 5))
print(mymath.sub(10, 5))β Python imports mymath.py from the same folder.
2. import Statement Variations π―
Import entire module
import_all.py
import math
print(math.sqrt(16))Import specific function
import_function.py
from math import sqrt
print(sqrt(16))Import multiple
import_multiple.py
from math import sqrt, pi
print(sqrt(25), pi)Import with alias
import_alias.py
import math as m
print(m.pi)Import specific functions with alias
import_func_alias.py
from math import sqrt as s
print(s(36))Note
β Alias shortens import names and improves readability.
3. Built-In Python Modules π
| Module | Purpose |
|---|---|
| math | Mathematical functions |
| random | Random numbers |
| datetime | Date & time handling |
| os | Operating system interaction |
| sys | System info & arguments |
| functools | Functional programming tools |
| json | Working with JSON data |
4. Examples of Built-In Modules π§
math
math_example.py
import math
print(math.pi)
print(math.sqrt(49))random
random_example.py
import random
print(random.randint(1, 10))
print(random.choice(["apple", "banana", "cherry"]))datetime
datetime_example.py
from datetime import datetime
now = datetime.now()
print(now)5. The dir() Function π
dir() lists all functions, variables, classes inside a module.
dir_example.py
import math
print(dir(math))6. Reloading Modules (Advanced) β»οΈ
reload_module.py
import mymath
import importlib
importlib.reload(mymath)Note
β Useful during development.
7. Module Search Path π
sys_path.py
import sys
print(sys.path)β Python looks for modules in these directories.
β You can add paths manually if needed.
8. Packages β Directories Containing Modules π
A package is a folder with multiple modules and an __init__.py file.
package_structure
mypackage/
__init__.py
mathops.py
stringops.pyImport from a package
import_package_module.py
from mypackage.mathops import add9. Creating __init__.py π§©
init_file.py
# inside __init__.py
from .mathops import add
from .stringops import uppercaseβ Makes package easier to import.
10. Real-World Examples π
Splitting Logic Into Modules
split_modules.py
# file: calculations.py
def add(a, b): return a + b
# file: main.py
import calculations
print(calculations.add(10, 20))Configuration in Separate Module
config_module.py
# config.py
API_KEY = "12345"
# main.py
import config
print(config.API_KEY)Utility Module
utils_example.py
# utils.py
def greet(name):
return f"Hello {name}"
# app.py
from utils import greet
print(greet("Sathish"))Conclusion π
>>βModules bring structure, organization, and reusability β helping you build clean and scalable Python applications.β β¨
You now fully understand Modules in Python! Want the next topic? Try Packages, OOP (Classes & Objects), File Handling, or Import System. Just tell me! π