What is a python init file? In this article we tell you everything you need to know about a Python init file, otherwise known as an __init__.py file. We discuss what the Python init file is used for, what needs to be put in a Python init file, and whether all Python packages need a Python init file.
What is __init__.py used for?
At its core, the __init__.py file is a file that is used to mark a directory as a Python package. Every directory that contains code that you want to be able to import as a simple Python package needs to contain an __init__.py. This includes subdirectories of directories that already have an __init__.py file in them.
The code in the __init__.py file is the first code that is run when a Python package is imported. This means that there are some special tricks that you can perform using and __init__.py file. We will discuss these more later on in this article.
Can __init__.py be empty?
One question we often get asked is whether an __init__.py file can be empty. The answer to the question is yes, an __init__.py file can be empty. In fact, if you do not have a specific use case that requires you to include code in your __init__.py file, then your __init__.py file should be empty. You should not add code to your __init__.py file just because you saw someone do so on the internet. You should view an empty __init__.py file as the default.
Do all Python packages need an __init__.py?
Do all Python packages need an __init__.py file? Regular Python packages require an __init__.py file in order to be considered packages, but there is a special type of package called a namespace package that does not require an __init__.py file to be present.
Namespace packages are great for situations where you have multiple sub-packages that you want to be able to import under the same top-level package name, but you want to maintain the sub-packages separately in different directories. For example, say you have two sub-packages called “addition” and “subtraction” that you want to be able to import under the top-level package “simple_math”. If you use namespace packages, then you can maintain your “addition” and “subtraction” packages separately but import them both under “simple_math” as so.
import simple_math.addition import simple_math.subtraction
You can read more about namespace packages here, but unless you have a reason to use a namespace package then you should just assume that you should use a regular package and include an __init__.py file in every directory.
What can you do with an __init__.py?
As we said before, the code in your __init__.py file is the first thing that runs when you import your Python package. This means that you can use it to trick Python into behaving in ways that it might not otherwise behave. Here are some common examples of uses for the __init__.py file.
- Initialize a logger. Do you have a logger that you want to initialize every time someone imports your Python package? You can do this by putting the initialization code for the logger in your __init__.py file.
- Import components from sub-packages. You can also use the __init__.py file to trick Python into importing items that are defined in sub_packages when you import your top-level package. For example, say you have a package called “animals” with a sub-package called “cow” that contains a class called “Cows”. You can add a line that imports the Cows class to your __init__.py package so that the “Cows” class automatically gets imported when you import your animals package, even if you do not import the “cow” sub-package.