It used to be a required part of a package (old, pre-3.3 "regular package", not newer 3.3+ "namespace package").

Here's the documentation.

Python defines two types of packages, regular packages and namespace packages. Regular packages are traditional packages as they existed in Python 3.2 and earlier. A regular package is typically implemented as a directory containing an __init__.py file. When a regular package is imported, this __init__.py file is implicitly executed, and the objects it defines are bound to names in the package’s namespace. The __init__.py file can contain the same Python code that any other module can contain, and Python will add some additional attributes to the module when it is imported.

But just click the link, it contains an example, more information, and an explanation of namespace packages, the kind of packages without __init__.py.

Answer from Loki on Stack Overflow
Top answer
1 of 14
2111

It used to be a required part of a package (old, pre-3.3 "regular package", not newer 3.3+ "namespace package").

Here's the documentation.

Python defines two types of packages, regular packages and namespace packages. Regular packages are traditional packages as they existed in Python 3.2 and earlier. A regular package is typically implemented as a directory containing an __init__.py file. When a regular package is imported, this __init__.py file is implicitly executed, and the objects it defines are bound to names in the package’s namespace. The __init__.py file can contain the same Python code that any other module can contain, and Python will add some additional attributes to the module when it is imported.

But just click the link, it contains an example, more information, and an explanation of namespace packages, the kind of packages without __init__.py.

2 of 14
1356

Files named __init__.py are used to mark directories on disk as Python package directories. If you have the files

mydir/spam/__init__.py
mydir/spam/module.py

and mydir is on your path, you can import the code in module.py as

import spam.module

or

from spam import module

If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.

The __init__.py file is usually empty, but can be used to export selected portions of the package under more convenient name, hold convenience functions, etc. Given the example above, the contents of the init module can be accessed as

import spam

This answer is based on this webpage.

🌐
Real Python
realpython.com › python-init-py
What Is Python's __init__.py For? – Real Python
July 9, 2025 - The __init__.py file is a Python source file, which means that it’s also a module. If you want to review the terms module and package and how they’re used in Python, then expand the collapsible section below:
Discussions

Still confused about what __init__ should be used for
In Python, a module is recognised if the folder has an init.py so it’s essentially saying “look at me, this folder has code inside”. If there’s no init.py, then Python doesn’t know there’s code inside, so won’t recognise the folder as something to import. It can be used completely blank (no import / code of any kind) for this purpose on its own. What init actually is, is a reserved method for initialising. This means that when you import this module, this code is automatically run. As such, if you have an init.py which has the line start = ‘Hello World’ … then the start variable will be accessible to you from the very first import. Otherwise you might have to explicitly write import module.start More on reddit.com
🌐 r/learnpython
10
73
October 16, 2021
What is __init__.py for?
The __init__.py file is a fundamental component in Python's package structure. It plays a crucial role in defining Python packages, initializing package modules, and controlling package behavior. Understanding __init__.py is essential for organizing your Python projects, managing namespaces, ... More on designgurus.io
🌐 designgurus.io
1
10
September 28, 2024
What do you have in your __init__.py files?
__init__.py is a requirement for a directory to be registered as a module. Usually, I don't put any content in it. Well no, not anymore. Since 3.3, python has had namespace packages which actually can't have an __init__.py file in it. For most purposes, you can have the file or not and it should work as expected, but they are not required anymore. Sometimes, I write imports clauses of classes and functions that are meant to be used in the code importing this module. This makes them clearly visible among other, implementation-details parts of the code. This is pretty good practice for anything that you want to be available as a top level object when people import your package. If you declare it, or import it, in __init__.py, then its available as a top level object of your package, otherwise, people would have to know which submodule to import in order to use those objects It's good to keep classes in a separate files. I'm not sure about that one, maybe it's a good convention for other languages that I started treating as a good habit in general, but I think it improves the readability and simplifies the structure of the package. Generally, yes. Sometimes its more convenient to put a class in the init.py, but you can always define it in another file and import it if you want to It's a short class extending an abstract class and it's not meant to be changed by anyone. I don't really know what does it have to do with anything. If people using this package are not supposed to, or not expected to, access this abstract class directly, then it should not be in the init.py file. This specific case should be placed in a utils file or one reserved for abstract base classes that your project uses __init__.py is executed immediately after the import, it can have initialization code. Registering a class structure is sort of initialization meant for that package. Yes but also no. Yes, the code in that file is ran when you import the package, or any part of it, and it can have initialization code. No, registering a class structure, especially if it is an internal structure, is not the correct type of initialization code to put in here. However, when I consider reasons for and against mentioned above, I'm coming to the conclusion it's better not to do anything besides top-level imports in __init__.py It depends. Check out requests . In their init.py they are running some basic checks to make sure that requests can work as expected, and throws some warnings if it can't. This is more than just doing top level imports, but at the same time it needs to go in init.py because it affects how the entire package can run. But really it all comes down to personal opinion. You can include more in init.py if you want, you can have nothing there. Some situations have stronger arguments in favor of each position, but you never have to put anything there if you don't want to More on reddit.com
🌐 r/learnpython
3
6
February 9, 2021
what can I put in __init__.py to make them useful
Lets say you have the following structure: - main.py - module.py With code in each file. This works fine. At some point you need to break up module.py into smaller chunks, like: - main.py - module/ - __init__.py - gui.py - encoding.py or something similar. However, because "module" is now a directory instead of a file, you can't "put code in it". The contents of __init__.py "belong" to the same namespace as module, so if module/__init__.py contains the following code: SOME_VAR = 5 then in main.py you can do: from module import SOME_VAR print(SOME_VAR + 37) Adding due to popularity: Because of the above, it's common to see __init__.py used for package-related variables (all the dunders like __author__, __version__, etc.) as well as "up-importing". "Up-importing" is a way to balance a simple user interface with a practical development layout. For example, it's not uncommon to put classes in their own module, so you might ahve: package/ models/ api.py db.py user.py and so on. However, if you do this, then the user needs to: from package.models.api import MyAPI from package.models.db import UserTable ... So instead, you make a package/__init__.py that contains the above (what you don't want to force the user to type): from package.models.api import MyAPI from package.models.db import UserTable ... and then the user can now do: from package import MyAPI, UserTable, ... Because all those objects have been "up-imported" into the top-level package namespace. For similar reasons, this is a good place to define __all__ = [], if you need it. More on reddit.com
🌐 r/learnpython
19
90
August 30, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › what-is-__init__-py-file-in-python
What is __Init__.Py File in Python? - GeeksforGeeks
July 23, 2025 - __init__.py is a special file used in Python to define packages and initialize their namespaces. It can contain an initialization code that runs when the package is imported. Without this file, Python won't recognize a directory as a package.
🌐
Sentry
sentry.io › sentry answers › python › what is `__init__.py` for in python?
What is `__init__.py` for in Python? | Sentry
December 15, 2023 - In Python projects, if you create a file called __init__.py in a directory then Python will treat that directory as a package. A package in Python is a collection of modules (individual .py files) that can be imported into other Python files.
🌐
Better Stack
betterstack.com › community › questions › what-is-init-py-for
What is init.py for? | Better Stack Community
October 5, 2023 - In Python, the __init__.py file is used to mark a directory as a Python package.
🌐
AlgoMaster
algomaster.io › learn › python › init-file
Init File | Python | AlgoMaster.io | AlgoMaster.io
January 3, 2026 - Let's dive into the depths of __init__.py and uncover its secrets together. At its core, the __init__.py file signifies that a directory is a Python package.
Find elsewhere
🌐
Career Karma
careerkarma.com › blog › python › what is __init__.py? : a guide
What is __init__.py? | Career Karma
December 30, 2020 - The __init__.py file in a Python project is used to configure certain imports in Python projects or packages. Learn the basics on how to use it on Career Karma.
🌐
Python Engineer
python-engineer.com › posts › init-py-file
What is __init__.py file in Python - Python Engineer
There are two types of packages in python, regular and namespace packages. The former requires __init__.py file whereas the latter does not. Any directory with an init python file is marked as a package by python and can be imported.
🌐
Medium
medium.com › @kay.herklotz › to-code-or-not-to-code-in-init-py-weighing-the-pros-and-cons-a35f158d388a
To Code or Not to Code in __init__.py: Weighing the Pros and Cons | by Kay Herklotz | Medium
November 15, 2024 - In Python, the __init__.py file plays a pivotal role in package initialization. It can be an empty file or contain code that sets up your package when it’s imported. As a software engineer, you might wonder whether to include code in this ...
Top answer
1 of 1
10
The __init__.py file is a fundamental component in Python's package structure. It plays a crucial role in defining Python packages, initializing package modules, and controlling package behavior. Understanding __init__.py is essential for organizing your Python projects, managing namespaces, and facilitating modular programming. This comprehensive guide will delve into the purpose, functionality, and best practices related to __init__.py. What is __init__.py? __init__.py is a special Python file that serves multiple purposes within Python packages: Package Identification: It signals to Python that the directory should be treated as a package. Initialization Code: It can execute initialization code for the package. Namespace Control: It can control which modules and attributes are exposed when the package is imported. The double underscores (__) denote that __init__.py is a "dunder" (double underscore) or special method in Python. Historical Context In versions of Python prior to 3.3, the presence of an __init__.py file was mandatory for a directory to be recognized as a Python package. Without it, attempting to import modules from the directory would result in an ImportError. Starting with Python 3.3, PEP 420 introduced implicit namespace packages, allowing directories without an __init__.py to be recognized as packages. This change provided more flexibility in organizing large projects and distributing packages across multiple directories or distributions. However, even with implicit namespace packages, including an __init__.py file is still common practice for several reasons, such as initializing package-level variables or controlling the package's public interface. Primary Purposes of __init__.py 1. Defining a Python Package A Python package is a directory containing Python modules (i.e., .py files). To explicitly declare a directory as a package, an __init__.py file is placed inside it. Without __init__.py: project/ utils/ helper.py Attempting to import: import utils.helper # Raises ImportError in Python < 3.3 With __init__.py: project/ utils/ __init__.py helper.py Now, the import works as expected: import utils.helper # Successfully imports helper.py 2. Package Initialization Code __init__.py can contain Python code that initializes the package. This code is executed the first time the package or any of its submodules is imported. Example: # project/utils/__init__.py print("Initializing the utils package") # Initialize package-level variables package_version = "1.0.0" # Setup logging for the package import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.info("Utils package loaded") Usage: import utils # Output: # Initializing the utils package # INFO:utils:Utils package loaded 3. Controlling Imports By defining the __all__ list in __init__.py, you can control which modules and attributes are exported when from package import * is used. Example: # project/utils/__init__.py __all__ = ['helper', 'calculator'] from .helper import HelperClass from .calculator import CalculatorClass Usage: from utils import * # Only helper and calculator are imported helper_instance = HelperClass() calculator_instance = CalculatorClass() 4. Namespace Packages While implicit namespace packages (PEP 420) allow packages without __init__.py, explicit namespace packages can still be created by including an __init__.py that declares a namespace. Example: # project/src/utils/__init__.py __path__ = __import__('pkgutil').extend_path(__path__, __name__) This allows multiple directories to contribute to the same namespace package. Creating and Using __init__.py Basic __init__.py A basic __init__.py can be empty or contain simple initialization code. Example: project/ utils/ __init__.py # Empty file helper.py Usage: import utils.helper # Imports helper.py Executing Initialization Code You can include code in __init__.py that needs to run when the package is imported. Example: # project/utils/__init__.py import sys print("Utils package is being imported") # Modify the system path sys.path.append('/additional/path') Usage: import utils # Output: # Utils package is being imported Exposing Submodules and Attributes By importing submodules or specific attributes in __init__.py, you can simplify the import statements for users of your package. Example: # project/utils/__init__.py from .helper import HelperClass from .calculator import CalculatorClass __all__ = ['HelperClass', 'CalculatorClass'] Usage: from utils import HelperClass, CalculatorClass helper = HelperClass() calculator = CalculatorClass() This approach allows users to access HelperClass and CalculatorClass directly from the utils package without specifying submodules.
🌐
Medium
sarangsurve.medium.com › python-basics-why-use-init-py-c88589e44c91
Python Basics: Why use __init__.py? | by Sarang Surve | Medium
February 4, 2024 - ... __init__.py is a fundamental building block for creating structured and organized Python packages. It’s like the invisible glue that holds your code together, making it easier to manage reuse, and share.
🌐
Reddit
reddit.com › r/learnpython › what do you have in your __init__.py files?
r/learnpython on Reddit: What do you have in your __init__.py files?
February 9, 2021 -

Today at work I had an argument with a senior developer from my team. The reason was him putting a class body into a package __init__.py file. I pointed out it shouldn't be there and I'd never expect to find it there. He claims there's nothing wrong with putting a short class like this in init file. At this point I should probably mention both of us have similar (low) level of experience with Python, as we transitioned from Java monolithic projects to Python microservices quite recently. We couldn't find an agreement.

My points for not putting class definitions in init and moving them to the separate files was:

  • __init__.py is a requirement for a directory to be registered as a module. Usually, I don't put any content in it. Sometimes, I write imports clauses of classes and functions that are meant to be used in the code importing this module. This makes them clearly visible among other, implementation-details parts of the code.

  • It's good to keep classes in a separate files. I'm not sure about that one, maybe it's a good convention for other languages that I started treating as a good habit in general, but I think it improves the readability and simplifies the structure of the package.

His points were:

  • It's a short class extending an abstract class and it's not meant to be changed by anyone. I don't really know what does it have to do with anything.

  • __init__.py is executed immediately after the import, it can have initialization code. Registering a class structure is sort of initialization meant for that package.

  • IDE will find the declaration for you anyway, no matter where it is.

To me, his behavior is even more unconditioned, because the class definition looks like this:

from .package.a import a
from .package.b import b

class Foo:
    def a(self, x):
        return a(x)
    def b(self, b):
        return b(x)

As you can see, class methods are imported from the separate files. If you're already using imports at that level, why don't just import the whole class? I don't get it...

Unluckily, I was unable to find any PEPs or other official recommendations on what should actually be stored in `__init__.py` file. Looking through the answers on StackOverflow I get mixed responses. Some people do actually put classes or other definitions there, some leave it empty or just do basic imports. To me, it's a subjective matter and it's up to you on how do you decide to use this file. However, when I consider reasons for and against mentioned above, I'm coming to the conclusion it's better not to do anything besides top-level imports in __init__.py. What is your opinion? Can you share any popular code bases utilizing init files in any other way than doing just simple imports?

EDIT: formatting

Top answer
1 of 2
4
__init__.py is a requirement for a directory to be registered as a module. Usually, I don't put any content in it. Well no, not anymore. Since 3.3, python has had namespace packages which actually can't have an __init__.py file in it. For most purposes, you can have the file or not and it should work as expected, but they are not required anymore. Sometimes, I write imports clauses of classes and functions that are meant to be used in the code importing this module. This makes them clearly visible among other, implementation-details parts of the code. This is pretty good practice for anything that you want to be available as a top level object when people import your package. If you declare it, or import it, in __init__.py, then its available as a top level object of your package, otherwise, people would have to know which submodule to import in order to use those objects It's good to keep classes in a separate files. I'm not sure about that one, maybe it's a good convention for other languages that I started treating as a good habit in general, but I think it improves the readability and simplifies the structure of the package. Generally, yes. Sometimes its more convenient to put a class in the init.py, but you can always define it in another file and import it if you want to It's a short class extending an abstract class and it's not meant to be changed by anyone. I don't really know what does it have to do with anything. If people using this package are not supposed to, or not expected to, access this abstract class directly, then it should not be in the init.py file. This specific case should be placed in a utils file or one reserved for abstract base classes that your project uses __init__.py is executed immediately after the import, it can have initialization code. Registering a class structure is sort of initialization meant for that package. Yes but also no. Yes, the code in that file is ran when you import the package, or any part of it, and it can have initialization code. No, registering a class structure, especially if it is an internal structure, is not the correct type of initialization code to put in here. However, when I consider reasons for and against mentioned above, I'm coming to the conclusion it's better not to do anything besides top-level imports in __init__.py It depends. Check out requests . In their init.py they are running some basic checks to make sure that requests can work as expected, and throws some warnings if it can't. This is more than just doing top level imports, but at the same time it needs to go in init.py because it affects how the entire package can run. But really it all comes down to personal opinion. You can include more in init.py if you want, you can have nothing there. Some situations have stronger arguments in favor of each position, but you never have to put anything there if you don't want to
2 of 2
1
This might be better suited in r/python .
🌐
Python documentation
docs.python.org › 3 › tutorial › modules.html
6. Modules — Python 3.14.4 documentation
A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement. [1] (They are also run if the file is executed as a script.)
🌐
LabEx
labex.io › tutorials › python-how-to-properly-set-up-an-init-py-file-in-a-python-package-398237
How to properly set up an __init__.py file in a Python package | LabEx
In Python programming, the __init__.py file serves as a marker that tells Python that a directory should be treated as a package. This file enables you to organize related code into a structured and reusable format.
🌐
LabEx
labex.io › tutorials › python-how-to-use-init-files-in-python-420195
How to use init files in Python | LabEx
This tutorial explores the fundamental ... architecture and import mechanisms. The __init__.py file is a special Python file that serves as an initializer for Python packages....
🌐
Reddit
reddit.com › r/learnpython › what can i put in __init__.py to make them useful
r/learnpython on Reddit: what can I put in __init__.py to make them useful
August 30, 2024 -

I recently learned (thanks wholly to others on this sub) how to organize my previously monolithic single .py file projects into modular directories (using poetry to make the project a package). I've been going at it all day, and this new world is very liberating and exciting!

But, now I have a bunch of empty __init__.py files in my project. Can I do anything useful with them? Do they work anything like class init, like 'execute everything in here first' when calling anything inside the same dir?

what can they be used for besides letting python know its a package structure?

Top answer
1 of 5
182
Lets say you have the following structure: - main.py - module.py With code in each file. This works fine. At some point you need to break up module.py into smaller chunks, like: - main.py - module/ - __init__.py - gui.py - encoding.py or something similar. However, because "module" is now a directory instead of a file, you can't "put code in it". The contents of __init__.py "belong" to the same namespace as module, so if module/__init__.py contains the following code: SOME_VAR = 5 then in main.py you can do: from module import SOME_VAR print(SOME_VAR + 37) Adding due to popularity: Because of the above, it's common to see __init__.py used for package-related variables (all the dunders like __author__, __version__, etc.) as well as "up-importing". "Up-importing" is a way to balance a simple user interface with a practical development layout. For example, it's not uncommon to put classes in their own module, so you might ahve: package/ models/ api.py db.py user.py and so on. However, if you do this, then the user needs to: from package.models.api import MyAPI from package.models.db import UserTable ... So instead, you make a package/__init__.py that contains the above (what you don't want to force the user to type): from package.models.api import MyAPI from package.models.db import UserTable ... and then the user can now do: from package import MyAPI, UserTable, ... Because all those objects have been "up-imported" into the top-level package namespace. For similar reasons, this is a good place to define __all__ = [], if you need it.
2 of 5
24
So, as of Python 3.3, you don't actually need these files anymore. Python can figure it out for the most part. Technically having the empty file gives a bit more information, but for most purposes that information is unnecessary. That being said, there is some functionality you can add to them. You can explicitly define your project structure in a way that makes it a bit easier to use. For example, lets say you had a folder called my_package and two subfolders called calculations and data_structures. In those folders you have vector_math.py and vector.py. Without the init.py files, someone importing your package that wants these classes (assuming they have class names) would need to do something like this: from my_package.calculations.vector_math import VectorMath from my_package.data_structures.vector import Vector This works but is fairly verbose. If you instead modify a init.py file for your my_package folder, you could add this: from .calculations.vector_math import VectorMath from .data_structures.vector import Vector Then, in the calculations folder, you'd have another init.py with this: from .vector_math import VectorMath Similar for data_structures folder: from .vector import Vector What does this do? It "exposes" your internal classes to the main package, allowing the user to do something like this instead of the original version: from my_package import VectorMath, Vector This makes it easier to use the package overall and gives you more flexibility in what sorts of imports your users can use. Basically, if you want to give the user a different structure than the one you are using internally, you need to use init.py files. There are some other features, for example you can set module-level variables or code you want to run on initialization of the module, but adjusting import paths is the main one in my opinion and the most commonly used. Another common usage is setting the __all__ list to determine what gets imported when you use from my_module import *, letting you avoid importing strictly internal classes or functions. For example, if you had a function that you didn't want to expose like _recursive_vector_functions.py, you could set `__all__ = ["VectorMath", "Vector"] to exclude it. And obviously if you plan to use a Python version before 3.3, you'll need them even if they are empty. There is one downside to using them, though: for large projects with complex file structures, these pre-processing steps can potentially make the whole import process take longer, reducing startup performance for projects that use your package. Most of the time the difference will be negligible, but it isn't "cost-free," so to speak. Hope that helps!
🌐
Christoolivier
blog.christoolivier.com › p › what-is-up-with-__init__-py-and-__main__-py
What's up with __init__.py and __main__.py ? - Christo's Blog
May 18, 2020 - The Hitchhiker's Guide to Python - Structuring Your Project ... So, let's get have a look at __init__.py. As per the Python documentation: The __init__.py files are required to make Python treat directories containing the file as packages.
🌐
Leapcell
leapcell.io › blog › understanding-init-py-in-python-packages
Understanding `__init__.py` in Python Packages | Leapcell
July 25, 2025 - In this article, we’ll explore what __init__.py does, why it exists, and how you can use it effectively. __init__.py is a special Python file that is used to mark a directory as a Python package.
🌐
Yasoob Khalid
yasoob.me › 2013 › 07 › 28 › what-is-__init__-py
What is __init__.py ? - Yasoob Khalid
The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module ...