Python Modules and Packages
Welcome to Python Modules and Packages at Ugenz Academy!
As your Python programs become larger, keeping all your code in one file can become difficult. Modules and packages help you organize code into smaller, reusable parts.
A module is usually a Python file, while a package is a directory that groups related Python modules together.
What You Will Learn
- Modules: Organize Python code in separate
.pyfiles. - Importing Modules: Reuse functions, variables, and classes with
import. - Built-in Modules: Explore modules such as
math,random, anddatetime. - Packages: Group related modules into directories.
- Third-Party Packages: Use external libraries in your projects.
- pip: Install and manage packages from the Python Package Index (PyPI).
Example: Importing a Module
Python includes many useful built-in modules.
import math print(math.sqrt(25)) print(math.pi)
Output:
5.0 3.141592653589793
The math module provides mathematical functions and constants.
Example: Creating Your Own Module
Create a file named calculator.py:
def add(a, b): return a + b def subtract(a, b): return a - b
Now create another file named main.py in the same directory:
import calculator print(calculator.add(10, 5)) print(calculator.subtract(10, 5))
Output:
15 5
Your main.py file can reuse functions defined in calculator.py.
Understanding Packages
A package groups related Python modules into a directory.
Example structure:
my_project/ ├── main.py └── utilities/ ├── __init__.py ├── calculator.py └── greetings.py
You can import a function from a module inside the package:
from utilities.calculator import add print(add(10, 20))
The __init__.py file is commonly used to mark and initialize a regular Python package, although modern Python also supports namespace packages without it.
Installing Third-Party Packages
You can install external packages using pip.
For example:
python -m pip install requests
Then use the package in your Python program:
import requests response = requests.get("https://example.com", timeout=10) print(response.status_code)
Use third-party packages from trusted sources and check their documentation before adding them to your projects.
Modules vs Packages
| Feature | Module | Package |
|---|---|---|
| Structure | Usually a .py file | Usually a directory |
| Purpose | Organizes related code | Groups related modules |
| Example | calculator.py | utilities/ |
| Import example | import calculator | from utilities import calculator |
Recommended Learning Path
- Learn how to import modules.
- Explore Python's built-in modules.
- Create your own modules.
- Understand package structures and imports.
- Install and use third-party packages with
pip.
Practice Challenge
Create a utilities package containing separate modules for mathematical operations and greeting messages. Import their functions into main.py and test them.
Next step: Learn how to organize larger Python projects before moving on to file handling and exception handling.
Learn • Build • Grow — Ugenz Academy