Python Full Tutorial — Part 10: Modules & Packages

By Suraj Ahir January 06, 2026 6 min read

Python — Python Decorators
Python — Python Decorators
← Part 9 Python Tutorial · Part 10 of 12 Part 11 →

Professional Python code is never one massive file. It is organized into modules and packages. This makes code reusable, testable, maintainable, and collaborative. Understanding the module system is essential before you start working on real projects.

What is a Module?

A module is simply a Python file (.py) containing functions, classes, and variables. Any Python file can be a module.

math_utils.py — Your Custom Module
# math_utils.py
def add(a, b):
    return a + b

def multiply(a, b):
    return a * b

PI = 3.14159
Using Your Module
# main.py
import math_utils

result = math_utils.add(5, 3)
print(result)  # 8
print(math_utils.PI)

import Styles

Different Import Ways
import math                    # Full import
print(math.sqrt(16))           # Use with prefix

from math import sqrt, pi      # Import specific items
print(sqrt(25))                # Use directly

import numpy as np             # Import with alias
arr = np.array([1, 2, 3])

Python's Standard Library — Built-in Power

Python comes with a massive standard library. No installation needed:

Standard Library Examples
import os
print(os.getcwd())        # Current directory
os.makedirs("data", exist_ok=True)  # Create folder

import datetime
now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d %H:%M:%S"))

import random
print(random.randint(1, 100))
print(random.choice(["red", "blue", "green"]))

import json
data = json.dumps({"name": "Suraj", "age": 22})

import sys
print(sys.version)
print(sys.platform)

pip — Installing Third-Party Packages

pip is Python's package manager. It connects to PyPI (Python Package Index), which has over 400,000 packages:

pip Commands
pip install requests          # Install a package
pip install requests==2.28.0  # Install specific version
pip uninstall requests        # Remove a package
pip list                      # Show installed packages
pip show requests             # Info about a package
pip install -r requirements.txt  # Install from file

requirements.txt — Sharing Dependencies

When sharing a project, list all dependencies:

requirements.txt
requests==2.28.2
pandas==2.0.1
flask==2.3.2
python-dotenv==1.0.0

Packages — Organized Collections of Modules

A package is a folder containing multiple modules and a special __init__.py file:

Package Structure
my_project/
├── main.py
├── utils/
│   ├── __init__.py
│   ├── math_utils.py
│   └── string_utils.py
└── data/
    └── config.json

In Part 11, we cover Object-Oriented Programming — the paradigm that powers most production Python codebases.

Common Mistakes Beginners Make

Learning from common pitfalls saves hours of frustration. Here are the mistakes most beginners make in this area and how to avoid them.

The first mistake is trying to memorize syntax before understanding logic. Python syntax is simple — you could memorize it in a day. But if you do not understand why you are writing what you are writing, you will not be able to adapt when things change or when a problem looks slightly different. Always ask: what problem does this solve?

The second mistake is writing very long functions or very long scripts without breaking them into logical units. Real professional code is made of small, focused pieces that each do one thing well. The moment your code does two or three unrelated things in one block, it is time to split it up.

The third mistake is not reading error messages. Python's error messages are actually quite good. They tell you the file, the line number, the type of error, and often a description. Read the entire error before searching online. The answer is usually right there.

How This Topic Appears in Real Projects

In real codebases, every concept from this tutorial series appears constantly. Backend web applications built with Flask or Django use functions, classes, data structures, and error handling throughout every route and service. Data pipelines use loops and comprehensions to process thousands of records efficiently. CLI tools use argument parsing, file handling, and process management. DevOps automation scripts combine shell integration with Python logic to orchestrate deployments, monitor systems, and handle alerts.

The concepts that feel abstract right now will click into place the moment you start building something real. That is why the best way to learn is to pick a small project that solves a problem you actually have — even something simple like a personal expense tracker or a file organizer — and build it using everything you have learned so far.

Practice Exercise

Before moving to the next part, write a small program that uses what you learned in this section. Do not copy from anywhere. Start with a blank file and build it from memory. The struggle is the learning. If you get stuck, read the code examples again — do not just copy them. Understand each line, then close the examples and write the program yourself. This is how programming actually sticks.

The Standard Library — Python's Hidden Wealth

Python ships with an extensive standard library — a collection of modules that cover an enormous range of common programming tasks without requiring any additional installation. Understanding what is available in the standard library prevents you from reinventing functionality that already exists and is well-tested. Key standard library modules worth knowing: datetime for date and time operations, os and pathlib for file system operations, json for JSON serialization, csv for CSV file handling, collections for specialized data structures like Counter, defaultdict, and namedtuple, itertools for efficient iteration tools, functools for higher-order functions, re for regular expressions, and logging for application logging.

Popular Third-Party Packages

Beyond the standard library, Python's ecosystem of third-party packages (installed via pip) is one of its greatest strengths. The most important packages to be aware of depend on your domain, but broadly useful ones include: requests for making HTTP requests to web APIs, pandas for data manipulation and analysis, numpy for numerical computing, pytest for testing, python-dotenv for managing environment variables and configuration, and click or argparse for building command-line tools. Learning to find, evaluate, and use third-party packages — checking PyPI, reading documentation, understanding dependency management — is an essential Python professional skill.

Practice Exercise

Write a Python script that uses the os module to list all files in a directory, the datetime module to format the current date and time, and the collections.Counter to count the frequency of file extensions in the directory. This exercise introduces the standard library in a practical, useful context that touches three different modules.

Disclaimer: This content is for educational purposes only. SRJahir Tech does not guarantee any specific outcome, job placement, or exam result. Learning requires consistent effort and practical application.