Python __init__.py proved to be irrelevant in version 3.4?

Question:

I am running python 3.4 on the main.py file in the same directory.
/root directory is not in python path. It is simply the current directory that the script is executing in. All pycache folders were deleted after each test

So why exactly is __init__.py important? I thought it was necessary as stated in this post:

What is __init__.py for?

If you remove the init.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.

Right now, it seems to me that __init__.py is nothing more than an optional constructor where we do housekeeping and other optional things like specifying the "all" variable, etc. But not a critical item to have.

Image showing the results of the test:

enter image description here

Can someone explain the discrepancy or what is the cause of this issue?

Asked By: AlanSTACK

||

Answers:

Found Answer

In essence, init.py is not needed, and its purpose is for legacy and optional housekeeping tasks that you may or may not want or need in Python versions 2.7 vs 3.0+. However, it is important to take into account that they have slightly different behavior during more complex parsing if you are building something more complex.

Please refer to the following links for additional reading material:

https://www.python.org/dev/peps/pep-0420/#namespace-packages-today

How do I create a namespace package in Python?

What's the difference between a Python module and a Python package?

https://softwareengineering.stackexchange.com/questions/276888/python-namespace-vs-module-with-underscores

Answered By: AlanSTACK

As confusing as it may be, although the basics will work without __init__.py files, you should probably still use them. Many external tools, as well as package-related functions in the standard library, will not work as expected without them. More words of wisdom here (as well as a misleading accepted answer): Is __init__.py not required for packages in Python 3.3+.

Answered By: lapis
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.