Skip to main content

How to Make a Folder into a Python Package with __init__.py

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A Python package is a folder of Python files that you import as one unit, such as shapes with shapes.area inside it. What makes the folder a regular package is a file named __init__.py. This guide builds a tiny package on Python 3.15.2 and shows when that file runs.

Syntax​

Syntax
<project>/
├── <package>/
│ ├── __init__.py
│ └── <module>.py
└── <script>.py
Syntax
from <package>.<module> import <name>
PartWhat it means
<package>/The folder you import by name
__init__.pyMarks the folder as a regular package. It can be empty, and any code in it runs when the package is first imported
<module>.pyA Python file inside the package
from <package>.<module> import <name>Imports one name from a module inside the package

The Python tutorial explains: "The init.py files are required to make Python treat directories containing the file as packages (unless using a namespace package, a relatively advanced feature)." It adds: "In the simplest case, init.py can just be an empty file, but it can also execute initialization code for the package".

The Example​

A shapes package holds an area module with a square function. Its __init__.py has a docstring and prints one line, so you can see when it runs. A script next to the folder imports square and uses it.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Make the Package Folder​

Create a folder named shapes in your project, with these two files in it.

shapes/__init__.py
"""Functions that measure simple shapes."""

print("Importing the shapes package")

The shapes/init.py file in a macOS-style code window: a docstring that says Functions that measure simple shapes, and a print of Importing the shapes package.

The print is only there to show you when the file runs. A real package's __init__.py is often empty or holds just a docstring.

shapes/area.py
def square(side):
return side * side

Step 2: Use the Package​

Next to the shapes folder, not inside it, create use_shapes.py:

use_shapes.py
from shapes.area import square

print(square(4))

The project now looks like this:

python-basics/
python-basics/
├── shapes/
│ ├── __init__.py
│ └── area.py
└── use_shapes.py

Step 3: Run It​

Terminal
uv run python use_shapes.py
Output
Importing the shapes package
16

The terminal after uv run python use_shapes.py: Importing the shapes package, then 16.

Importing shapes.area ran shapes/__init__.py first, which printed its line, then loaded area.py, and square(4) returned 16. The command works unchanged in PowerShell.

Python found shapes because the folder of the script you run is the first place it looks for imports.

Common Mistakes​

MistakeWhat happensDo this instead
Putting the script inside the package folder, as shapes/use_shapes.pyPython searches the script's own folder, shapes/, for a shapes package and doesn't find one: ModuleNotFoundError: No module named 'shapes'Keep the script next to the package folder, not inside it
Expecting __init__.py to run on every importIt runs once: a script that imported shapes twice printed Importing the shapes package only oncePut one-time setup there, nothing that must repeat

FAQ​

What happens without __init__.py?​

The import still works: Python treats the folder as a namespace package. In a test, a folder without the file imported fine, and its __file__ was None. Namespace packages are meant for packages split across several folders; for an ordinary package, add __init__.py.

Can __init__.py make imports shorter?​

Yes. If shapes/__init__.py contains from shapes.area import square, users can write from shapes import square.

Conclusion​

Adding __init__.py made shapes a regular package. from shapes.area import square ran the package's __init__.py once, then imported the function, which returned 16.

Additional Resources​