Python Modules and Packages

Python 9 min min read Updated: Mar 09, 2026 Intermediate
Python Modules and Packages
Intermediate Topic 8 of 10

Modules & Packages in Python

As Python programs grow larger, managing code becomes more challenging. To organize code efficiently and promote reusability, Python provides the concepts of modules and packages. These allow developers to structure programs into smaller, manageable components.

Modules and packages help break large programs into multiple files and directories, making code easier to maintain, understand, and reuse.

What is a Module in Python?

A module is simply a Python file that contains functions, classes, and variables. Modules allow developers to reuse code across different programs without rewriting it.

For example, a module can contain functions related to mathematical operations or data processing.

python # math_utils.py def add(a, b): return a + b def multiply(a, b): return a * b

This file defines two functions inside a module named math_utils.

Importing Modules

To use a module in another Python file, we use the import statement.

python import math_utils print(math_utils.add(5, 3)) print(math_utils.multiply(4, 2))

This imports the module and allows access to its functions.

Importing Specific Functions

You can also import specific functions from a module.

python from math_utils import add print(add(10, 5))

This allows direct use of the function without referencing the module name.

Using Aliases

Modules can also be imported with shorter names using aliases.

python import math_utils as mu print(mu.add(2, 3))

This makes the code cleaner and easier to write.

Built-in Python Modules

Python provides many built-in modules that offer useful functionality.

Some commonly used modules include:

  • math – mathematical functions
  • random – random number generation
  • datetime – date and time operations
  • os – operating system interactions
  • sys – system-specific functions
python import math print(math.sqrt(16))

What is a Package in Python?

A package is a collection of multiple modules organized in a directory. It helps structure large Python projects logically.

A package usually contains an __init__.py file that indicates the directory should be treated as a Python package.

Example Package Structure

myproject/
│
├── main.py
│
└── utilities/
    ├── __init__.py
    ├── math_utils.py
    └── string_utils.py

Here, utilities is a package containing multiple modules.

Importing from Packages

You can import modules from packages using the dot notation.

python from utilities.math_utils import add print(add(10, 20))

This imports the add function from the package module.

Installing External Packages

Python also allows installing third-party packages using the pip package manager.

bash pip install requests

Once installed, the package can be imported into your Python program.

python import requests response = requests.get("https://example.com") print(response.status_code)

Real-World Example

In large applications such as web frameworks or machine learning systems, packages are used to organize modules related to different functionalities.

python import datetime current_time = datetime.datetime.now() print("Current time:", current_time)

This example uses Python's built-in datetime module.

Best Practices for Using Modules & Packages

  • Keep modules small and focused on a single responsibility.
  • Use meaningful module and package names.
  • Group related modules inside packages.
  • Avoid circular imports between modules.

Conclusion

Modules and packages are essential for organizing Python code into reusable and maintainable components. Modules allow developers to reuse code across multiple programs, while packages help structure larger projects logically.

Understanding how to create and use modules and packages will help you build scalable and well-structured Python applications.

In the next tutorial, we will explore Python Standard Library and learn about the built-in modules that make Python powerful.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators