Python Modules and Packages Tutorial | Import Modules, Create Packages & Code Reusability
As software applications grow larger, keeping all the code in a single file becomes difficult to manage. Professional developers organize their programs into modules and packages, making applications easier to maintain, debug, and reuse.
Python provides a simple yet powerful way to split code into multiple files using modules and organize related modules into packages. This modular approach is widely used in web development, artificial intelligence, automation, and enterprise software development.
In this lesson, you’ll learn how to import built-in modules, create custom modules, organize code into packages, use the Python Standard Library, and follow best practices for structuring professional Python projects.
By the end of this lesson, you’ll be able to organize your code efficiently and build scalable Python applications.
Learning Objectives
After completing this lesson, you will be able to:
- Understand modules and packages.
- Import built-in modules.
- Create custom modules.
- Import specific functions.
- Use aliases for modules.
- Create Python packages.
- Explore the Python Standard Library.
- Organize large projects professionally.
Topics Covered
- Introduction to Modules
- Why Modules are Important
- Importing Modules
- Built-in Modules
- Creating Custom Modules
- Different Import Methods
- Aliases
- Packages
- Python Standard Library
- Best Practices
Detailed Lesson Content
Introduction to Modules
Imagine you’re building an E-Commerce Website.
Instead of writing everything in one file, professional developers separate the application into different files.
For example:
Authentication.py
Payment.py
Products.py
Orders.py
Customers.py
Each file performs a specific task.
These files are called Modules.
A module is simply a Python file (.py) containing functions, variables, or classes that can be reused in other programs.
Modules help developers write cleaner, organized, and reusable code.
Why Use Modules?
Without modules:
- Large programs become difficult to manage.
- Code gets duplicated.
- Maintenance becomes challenging.
- Debugging takes longer.
With modules:
- Better organization.
- Easy code reuse.
- Faster development.
- Improved readability.
- Easier collaboration.
Importing Built-in Modules
Python comes with hundreds of ready-to-use modules.
To use a module:
import math
Example:
import math
print(math.sqrt(25))
Output
5.0
Common Built-in Modules
Math Module
import math
print(math.pi)
print(math.factorial(5))
Output
3.141592653589793
120
Useful functions:
- sqrt()
- ceil()
- floor()
- factorial()
- pow()
Random Module
Generates random values.
import random
print(random.randint(1,100))
Example output
57
Useful in:
- Games
- OTP Generation
- AI Simulations
- Testing
Datetime Module
Works with dates and time.
import datetime
today = datetime.datetime.now()
print(today)
Applications:
- Attendance Systems
- Billing Software
- Booking Systems
- Reports
OS Module
Interacts with the operating system.
import os
print(os.getcwd())
Useful for:
- File Management
- Folder Operations
- Automation Scripts
Import Specific Functions
Instead of importing the entire module:
from math import sqrt
print(sqrt(49))
Output
7.0
This imports only the required function.
Import Multiple Functions
from math import sqrt, factorial
print(sqrt(64))
print(factorial(6))
Using Aliases
Aliases shorten long module names.
Example:
import datetime as dt
print(dt.datetime.now())
Another example
import numpy as np
This is a common practice in Data Science and AI development.
Creating Your Own Module
Suppose you create a file named:
calculator.py
Contents:
def add(a,b):
return a+b
def subtract(a,b):
return a-b
Now create another file:
main.py
Import the module:
import calculator
print(calculator.add(20,10))
Output
30
Your own Python file can now be reused anywhere.
Importing Specific Functions from Custom Modules
Instead of importing everything:
from calculator import add
print(add(5,10))
Output
15
What is a Package?
A package is a collection of related modules organized inside a folder.
Example:
StudentManagement/
│
├── __init__.py
├── student.py
├── marks.py
├── attendance.py
└── reports.py
Each module performs a different task.
Together they form a package.
The init.py File
A package usually contains an __init__.py file.
This tells Python that the folder should be treated as a package.
Although optional in newer versions of Python, it is still commonly used in professional projects for initialization and package configuration.
Importing from Packages
Folder Structure
Store/
products.py
products.py
def display():
print("Products Loaded")
main.py
from Store.products import display
display()
Output
Products Loaded
Python Standard Library
Python provides thousands of ready-made functions through its Standard Library.
Popular modules include:
| Module | Purpose |
|---|---|
| math | Mathematical operations |
| random | Random number generation |
| datetime | Date & Time |
| os | Operating System |
| sys | System Functions |
| json | JSON Handling |
| csv | CSV Files |
| statistics | Statistical Calculations |
| pathlib | File Paths |
| collections | Specialized Data Structures |
These modules help developers avoid writing common functionality from scratch.
Real-World Example 1: OTP Generator
import random
otp = random.randint(100000,999999)
print("Your OTP is:",otp)
Output
Your OTP is: 548932
Real-World Example 2: Age Calculator
import datetime
birth_year = 2005
current_year = datetime.datetime.now().year
age = current_year - birth_year
print(age)
Real-World Example 3: Area Calculator Module
geometry.py
def area_square(side):
return side*side
def area_circle(radius):
return 3.14*radius*radius
main.py
import geometry
print(geometry.area_square(5))
print(geometry.area_circle(10))
Output
25
314.0
Common Mistakes
Module Not Found
Incorrect
import calculater
Correct
import calculator
Forgetting the File Extension Rule
Incorrect
import calculator.py
Correct
import calculator
Circular Imports
Avoid importing two modules into each other unnecessarily, as this can lead to import errors and make the code harder to maintain.
Best Practices
- Keep one purpose per module.
- Use meaningful module names.
- Organize related modules into packages.
- Prefer importing only what you need.
- Avoid duplicate code.
- Follow Python naming conventions.
- Use the Standard Library whenever possible before creating your own solution.



