At what point and how often do __init__.py files execute with python

Can someone help and clarify when using the commands importat what point the __init__.py files are executed in various package directories?

  • For each included module?
  • Only once with the 1st team import?
  • For each team import?
+4
source share
1 answer

It is evaluated on the first import of the module. At the next import, intrpreter detects that the module is already loaded and simply returns a link to it. No need to re-execute the code.

Quoting Import System :

In caching modules:

, , - sys.modules. , . , foo.bar.baz , sys.modules foo, foo.bar foo.bar.baz. .

sys.modules, , , , . , None, ImportError. , Python .

__init__ :

Python , . - , Python 3.2 . , init.py. , __init__.py , . __init__.py Python, , Python .

+7

Source: https://habr.com/ru/post/1654347/


All Articles