Ugenz Academy/Tutorial

Python Modules and Packages

Learn how to organize, reuse, import, and manage Python code using modules, packages, and libraries.

3 min read472 words

Reading progress

Keep learning

What you'll learn

  • Understand what Python modules and packages are.
  • Create and import your own Python modules.
  • Use import and from-import statements.
  • Understand Python packages and their folder structure.
  • Explore built-in modules and external libraries.
  • Install third-party packages using pip.

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 .py files.
  • Importing Modules: Reuse functions, variables, and classes with import.
  • Built-in Modules: Explore modules such as math, random, and datetime.
  • 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

FeatureModulePackage
StructureUsually a .py fileUsually a directory
PurposeOrganizes related codeGroups related modules
Examplecalculator.pyutilities/
Import exampleimport calculatorfrom utilities import calculator

Recommended Learning Path

  1. Learn how to import modules.
  2. Explore Python's built-in modules.
  3. Create your own modules.
  4. Understand package structures and imports.
  5. 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

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy