Skip to content

Using Packages

In Python, a package is a way to organize and group several related modules into a folder structure. Packages help you keep your code organized, scalable, and easy to maintain, especially in large projects.


A package is simply a folder that contains a special file called __init__.py (can be empty) and one or more modules (.py). This tells Python that the folder should be treated as a package.

Example of a package structure:

  • Directorymy_package/
    • __init__.py
    • module1.py
    • module2.py

  • Organization: Groups related modules under the same folder.
  • Scalability: Makes it easier to grow your project without losing control.
  • Reuse: You can import only what you need from each package.

  1. Create the package folder For example, create a folder called utilities and inside it a file __init__.py (can be empty).

  2. Add your modules For example, create a file math.py inside the folder:

Module Inside a Package
# utilities/math.py
def add(a, b):
return a + b
  1. Import from your package In your main file, import the module or function you need:
Importing a Module from a Package
from utilities import math
print(math.add(3, 4)) # 7

You can also import specific functions:

Importing a Specific Function from a Package
from utilities.math import add
print(add(10, 5)) # 15

  • Use descriptive names for your packages and modules.
  • Keep each package focused on a single topic or functionality.
  • Place an __init__.py file in each package folder (even if it is empty).

  • Packages group related modules and help organize large projects.
  • You only need a folder with __init__.py and your modules.
  • Import modules or functions as needed.

Ready to practice? Create your own package with several modules and test it in a program.