in

How does a blank __init__.py change the behavior of import in python?


I’m seeing many examples online that seem to indicate mere inclusion of a blank __init__.py file in a folder turns from a namespace package into a package. I’d like to figure out what this actually means. Consider the following example:

root
└───a
        b.py
        __init__.py

The python files contain no code, as this is only for demonstration purposes. Whether I include the blank __init__.py file or not, there is no difference in the following:

Python 3.9.5 (default, May 18 2021, 14:42:02) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
>>> import a
>>> a.b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'a' has no attribute 'b'
>>> b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>> from a import b
>>> b
<module 'a.b' from 'C:Usersuser_iddesktoptemppackages_questionab.py'>

I understand that including a from . import b statement inside a_init_.py will change things, but that’s not what this question is about. I’m more interested in the role of a blank __init__.py. How does the blank __init__.py change the behavior of import in python?



Source: https://stackoverflow.com/questions/70721414/how-does-a-blank-init-py-change-the-behavior-of-import-in-python

Puca v2.4.10 – Optimized Mobile WooCommerce Theme – ULC

iPhone 14 All Models Expected to Arrive With 120Hz Refresh Rate, 6GB RAM