1

Does setuptools' find_packages still require that packages have an __init__.py file inside?

The documentation says

Packages are only recognized if they include an __init__.py file.

But I read that __init__.py files are not required anymore to mark a directory as a python package.

phd
  • 82,685
  • 13
  • 120
  • 165
Michele Piccolini
  • 2,634
  • 16
  • 29

1 Answers1

6

Update

Looking back, the original answer is misguided.

Package initializers (the __init__.py files) should always be there. Because of some implementation details, it is true that it might work without, but it is not something that was strictly intended and not something that should be relied on.

Some details:


Original answer

In recent version of Python the package initializers (the __init__.py files) are not strictly necessary, meaning that such packages can be imported and so on.

But setuptools is not Python, so to say. And in particular find_packages still bases its lookup on the presence of such files. On the other hand, setuptools also offers the alternative find_namespace_packages function, that is able to find packages that do not contain a package initializer.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • I would like to add the following directly from [PEP 420](https://www.python.org/dev/peps/pep-0420/#specification): "A namespace package is not fundamentally different from a regular package. It is just a different way of creating packages. Once a namespace package is created, there is no functional difference between it and a regular package." – Onilton Maciel Dec 04 '21 at 05:43
  • 1
    And also from [PEP 420](https://www.python.org/dev/peps/pep-0420/#discussion): "There is no intention to remove support of regular packages. If a developer knows that her package will never be a portion of a namespace package, then there is a performance advantage to it being a regular package (with an `__init__.py`). Creation and loading of a regular package can take place immediately when it is located along the path. With namespace packages, all entries in the path must be scanned before the package is created." – Onilton Maciel Dec 04 '21 at 05:44