Is not required for packages in Python 3.3+

ID : 10337

viewed : 47

Tags : pythonpython-3.xpackagepython

Top 5 Answer for Is not required for packages in Python 3.3+

vote vote



@Mike's answer is correct but too imprecise. It is true that Python 3.3+ supports Implicit Namespace Packages that allows it to create a package without an file. This is called a namespace package in contrast to a regular package which does have an file (empty or not empty).

However, creating a namespace package should ONLY be done if there is a need for it. For most use cases and developers out there, this doesn't apply so you should stick with EMPTY files regardless.

Namespace package use case

To demonstrate the difference between the two types of python packages, lets look at the following example:

google_pubsub/              <- Package 1     google/                 <- Namespace package (there is no         cloud/              <- Namespace package (there is no             pubsub/         <- Regular package (with        <- Required to make the package a regular package         google_storage/             <- Package 2     google/                 <- Namespace package (there is no         cloud/              <- Namespace package (there is no             storage/        <- Regular package (with        <- Required to make the package a regular package        

google_pubsub and google_storage are separate packages but they share the same namespace google/cloud. In order to share the same namespace, it is required to make each directory of the common path a namespace package, i.e. google/ and cloud/. This should be the only use case for creating namespace packages, otherwise, there is no need for it.

It's crucial that there are no __init__py files in the google and google/cloud directories so that both directories can be interpreted as namespace packages. In Python 3.3+ any directory on the sys.path with a name that matches the package name being looked for will be recognized as contributing modules and subpackages to that package. As a result, when you import both from google_pubsub and google_storage, the Python interpreter will be able to find them.

This is different from regular packages which are self-contained meaning all parts live in the same directory hierarchy. When importing a package and the Python interpreter encounters a subdirectory on the sys.path with an file, then it will create a single directory package containing only modules from that directory, rather than finding all appropriately named subdirectories outside that directory. This is perfectly fine for packages that don't want to share a namespace. I highly recommend taking a look at Traps for the Unwary in Python’s Import System to get a better understanding of how Python importing behaves with regular and namespace package and what traps to watch out for.


  • Only skip files if you want to create namespace packages. Only create namespace packages if you have different libraries that reside in different locations and you want them each to contribute a subpackage to the parent package, i.e. the namespace package.
  • Keep on adding empty __init__py to your directories because 99% of the time you just want to create regular packages. Also, Python tools out there such as mypy and pytest require empty files to interpret the code structure accordingly. This can lead to weird errors if not done with care.


My answer only touches the surface of how regular packages and namespace packages work, so take a look at the following resources for further information:

vote vote


Python 3.3+ has Implicit Namespace Packages that allow it to create a packages without an file.

Allowing implicit namespace packages means that the requirement to provide an file can be dropped completely, and affected ... .

The old way with files still works as in Python 2.

vote vote


If you have in your project and you use find_packages() within it, it is necessary to have an file in every directory for packages to be automatically found.

Packages are only recognized if they include an file

UPD: If you want to use implicit namespace packages without you just have to use find_namespace_packages() instead


vote vote


I would say that one should omit the only if one wants to have the implicit namespace package. If you don't know what it means, you probably don't want it and therefore you should continue to use the even in Python 3.

vote vote


Based on my experience, even with python 3.3+, an empty is still needed sometimes. One situation is when you want to refer a subfolder as a package. For example, when I ran python -m, it didn't work until I created an empty under the test folder. And I'm talking about 3.6.6 version here which is pretty recent.

Apart from that, even for reasons of compatibility with existing source code or project guidelines, its nice to have an empty in your package folder.

Top 3 video Explaining Is not required for packages in Python 3.3+