3

I have read that there is no longer a need to add the __init__.py file in latest versions of python to treat a folder as package. However, the python official documentation does not say this - for example the below still shows examples and documentation using the __init__.py file.

The __init__.py files are required to make Python treat directories containing the file as packages.

https://docs.python.org/3/tutorial/modules.html#packages

Do we still need to use the __init__.py file to make python treat a folder as package? And are there any advantages/disadvantages to adding/removing this file?

variable
  • 8,262
  • 9
  • 95
  • 215
  • 2
    Not sure I completely understand it all, but "namespace packages" don't need it whereas "regular packages" do, https://docs.python.org/3/reference/import.html#namespace-packages – gstukelj Oct 17 '19 at 12:53
  • Does this answer your question? [Is \_\_init\_\_.py not required for packages in Python 3.3+](https://stackoverflow.com/questions/37139786/is-init-py-not-required-for-packages-in-python-3-3) – Flimm Jan 11 '22 at 13:27

1 Answers1

3

That is True but only for namespace packages.

There are currently three different approaches to creating namespace packages:

  1. Use native namespace packages. This type of namespace package is defined in PEP 420 and is available in Python 3.3 and later. This is recommended if packages in your namespace only ever need to support Python 3 and installation via pip.
  2. Use pkgutil-style namespace packages. This is recommended for new packages that need to support Python 2 and 3 and installation via both pip and python setup.py install.
  3. Use pkg_resources-style namespace packages. This method is recommended if you need compatibility with packages already using this method or if your package needs to be zip-safe.

Maybe you mention the native namespace packages.

Frank
  • 1,959
  • 12
  • 27