πŸ“¦ 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.

1. Creating Your Own Module 🧱

Step 1: Create a Python file

Create a file named:

Code Snippet

mymath.py

Step 2: Add functions

mymath.py

def add(a, b):
    return a + b

def sub(a, b):
    return a - b

Step 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 πŸ“š

ModulePurpose
mathMathematical functions
randomRandom numbers
datetimeDate & time handling
osOperating system interaction
sysSystem info & arguments
functoolsFunctional programming tools
jsonWorking 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.py

Import from a package

import_package_module.py

from mypackage.mathops import add

9. 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! 😊