Python Packages

In the last lesson, we learned about Creating Python Modules. Now let us group related modules into packages so larger projects stay organized.

What Is a Package in Python?

A package is a folder that contains Python modules. It can also contain subpackages. It helps us group related code.

Think of a package like a folder on your computer. You put related files inside. Instead of having one big module, we split it into smaller modules and put them in a package.

Why Do We Need Packages in Python?

As our programs grow, we may have many modules. Putting them all in one folder becomes messy. Packages let us organize them into subfolders.

Packages also make it easier to share code. We can create a package and install it on other computers.

How to Create a Package in Python

A package is just a folder with a special file called __init__.py inside. That file can be empty. It tells Python that the folder is a package.

Let us create a package called mypackage.

mypackage/
__init__.py
math_utils.py
string_utils.py

The __init__.py file is empty. The math_utils.py and string_utils.py are modules.

How to Use a Package in Python

Now we can import modules from the package.

main.py
from mypackage import math_utils, string_utils
print(math_utils.add(5, 3))
print(string_utils.reverse_string("hello"))

Output

8
olleh

We can also import specific functions.

from mypackage.math_utils import add
print(add(5, 3))

Output

8

Adding Code to init.py

The __init__.py file can be empty. But we can also put code in it. That code runs when the package is imported.

mypackage/__init__.py
print("mypackage is being imported")

Now when we import the package, we see the message.

import mypackage

Output

mypackage is being imported

That is useful for setting up the package.

Subpackages in Python

A package can contain subpackages. A subpackage is a folder inside a package with its own __init__.py.

mypackage/
__init__.py
math_utils.py
string_utils.py
subpackage/
__init__.py
advanced_math.py

We can import from the subpackage.

from mypackage.subpackage import advanced_math
advanced_math.do_something()

That is how we organize large projects.

A Real-Life Example – A Utility Package

Let us create a package called utils with two modules: file_utils.py and string_utils.py.

utils/
__init__.py
file_utils.py
string_utils.py

file_utils.py:

def read_file(filename):
with open(filename, "r") as file:
return file.read()
def write_file(filename, content):
with open(filename, "w") as file:
file.write(content)

string_utils.py:

def reverse(s):
return s[::-1]
def capitalize_words(s):
return s.title()

Now we can use this package in our main program.

from utils import file_utils, string_utils
string_utils.write_file("test.txt", "hello world")
print(file_utils.read_file("test.txt"))
print(string_utils.reverse("Python"))
print(string_utils.capitalize_words("hello world"))

Output

hello world
nohtyP
Hello World

That is a clean and organized package.

Common Mistakes with Packages in Python

Mistake 1: Forgetting init.py

Without __init__.py, Python does not recognize the folder as a package.

Fix: Create an empty __init__.py file inside the folder.

Mistake 2: Using the wrong import path

from mypackage.math_utils import add

If the package is not in the same folder as the script, this fails.

Fix: Make sure the package is in the right place, or adjust sys.path.

Mistake 3: Naming a package the same as a built-in

Do not name your package math or random. It will conflict with built-in modules.

Mistake 4: Circular imports between packages

If package A imports package B and package B imports package A, we get circular imports.

Fix: Reorganize the code to avoid circular dependencies.


Next, we will learn about Python Virtual Environments and isolate each project’s dependencies.