🧱 Python Object-Oriented Programming (OOP)
Estimated reading: 3 minutes 267 views

Python Packages – Create, Use, and Organize Code

Introduction – Why Use Python Packages?

As your Python project grows, it’s important to organize your code into manageable, reusable components. That’s where Python packages come in.

A package is a directory that contains a collection of related modules, organized with an __init__.py file. Packages:

  • Help avoid name collisions
  • Promote modular programming
  • Make code easier to maintain and reuse

In this guide, you’ll learn:

  • What Python packages are
  • How to create and import packages
  • Package structure and __init__.py
  • Real-world examples and tips
  • Best practices for packaging your code

What Is a Python Package?

A package is a folder that contains an __init__.py file (can be empty), and one or more Python modules (.py files).

Example:

my_package/
β”‚
β”œβ”€β”€ __init__.py
β”œβ”€β”€ module1.py
└── module2.py

The __init__.py tells Python that this directory should be treated as a package.


Creating a Package – Step-by-Step

Step 1: Create Folder Structure

math_utils/
β”œβ”€β”€ __init__.py
β”œβ”€β”€ addition.py
└── subtraction.py

Step 2: Write Modules

addition.py:

def add(a, b):
    return a + b

subtraction.py:

def subtract(a, b):
    return a - b

__init__.py:

from .addition import add
from .subtraction import subtract

Step 3: Import and Use the Package

from math_utils import add, subtract

print(add(5, 3))       # Output: 8
print(subtract(5, 3))  # Output: 2

Explanation:

  • __init__.py exposes selected functions directly at the package level.

Importing from Packages

Syntax Examples:

import math_utils.addition
print(math_utils.addition.add(2, 3))

from math_utils import subtraction
print(subtraction.subtract(5, 2))

from math_utils.addition import add
print(add(1, 1))

Nested Packages (Subpackages)

You can nest packages inside packages:

data/
β”œβ”€β”€ __init__.py
β”œβ”€β”€ preprocess/
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── cleaner.py
└── analysis/
    β”œβ”€β”€ __init__.py
    └── stats.py

This allows hierarchical structure for large applications.


Real-World Example – File Toolkit

Structure:

file_toolkit/
β”œβ”€β”€ __init__.py
β”œβ”€β”€ reader.py
└── writer.py

reader.py:

def read_file(path):
    with open(path, 'r') as f:
        return f.read()

writer.py:

def write_file(path, content):
    with open(path, 'w') as f:
        f.write(content)

Usage:

from file_toolkit.reader import read_file
from file_toolkit.writer import write_file

write_file("test.txt", "Hello")
print(read_file("test.txt"))

Installing External Packages (pip)

To install third-party packages:

pip install numpy

Use in code:

import numpy as np
print(np.array([1, 2, 3]))

Best Practices for Python Packages

Do This Avoid This
Use __init__.py to expose APIsLeaving __init__.py empty unnecessarily
Follow PEP8 naming conventionsUsing inconsistent module names
Keep modules focused and modularPutting unrelated functions in one file
Use relative imports inside packagesOverusing absolute imports

Summary – Recap & Next Steps

Python packages allow you to organize, distribute, and reuse your code in a clean, maintainable way. With just a few folders and __init__.py files, you can build large applications efficiently.

Key Takeaways:

  • A package is a folder with an __init__.py file
  • Modules inside packages can be imported using dot syntax
  • __init__.py can expose public methods
  • Packages support modular, scalable codebases

Real-World Relevance:
Packages are critical for frameworks, APIs, libraries, and enterprise apps.


FAQ – Python Packages

What is the purpose of __init__.py?

It marks a directory as a Python package and can be used to control imports and expose selected members.

How do I import from a subpackage?

Use:

from package.subpackage.module import function

Can a package contain subpackages?

Yes. Packages can be nested to create complex hierarchies.

Are packages and modules the same?

No.

  • Module = a single .py file
  • Package = a folder containing one or more modules and __init__.py

How do I install a third-party package?

Use pip:

pip install package_name

Then:

import package_name

Share Now :
Share

Python Packages

Or Copy Link

CONTENTS
Scroll to Top