4 ms·
I often see empty `__init__.py` files in package folders at work despite nobody being able to demonstrate any difference from when they're removed. For example
by ryan-duve 5y ago
I often see empty `__init__.py` files in package folders at work despite nobody being able to demonstrate any difference from when they're removed. For example, this works with an `__init__.py`:
$ docker container run --rm -it --entrypoint bash python:3-slim
root@bf9368a0ccd6:/# mkdir folder
root@bf9368a0ccd6:/# echo "x = 7" > folder/source.py
root@bf9368a0ccd6:/# python
Python 3.10.2 (main, Jan 29 2022, 02:55:36) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from folder.source import x
>>> print(x)
7
Research on the need for empty `__init__.py` files always surfaces outdated articles back from when Python2 required them and/or what I can only describe as sentiment such as "because that's how it's always been done". Meanwhile, valid reasons to not include empty `__init__.py` files are:
1. Removing unneeded things simplifies the package code base.
2. When tracking down a package bug, if there is an `__init__.py` file in every folder, they have to be checked for logic potentially causing the issue.
Maybe someone on HN can give a clear answer relevant in 2022: what is the benefit of putting empty `__init__.py` files in Python3 package folders?
- JohnLockwood 5y agoThat's a good point. It was only when reading about namespace packages in PEP 420 that I realized they were no longer required. Honestly, I think adding them now is just a matter of folks not knowing they're no longer required.