Based on this post: What is __init__.py for?, I know that __init__.py
in a module has two functions: 1) make the directory into a package so that the module can be imported. 2) do some necessary initialization job, if any. My question is when this __init__.py
is executed? Every time the module is imported into a module? Or there is something like a shared library in the memory so that this __init__.py
is only executed once in the whole application?
Further, I do not find good resources on how python interpreter executes python code, any suggestions on this would be appreciated.
Asked
Active
Viewed 368 times
0
-
Use backticks for inline code formatting. \`\_\_init\_\_.py\` becomes `__init__.py`. – user2357112 Jul 27 '18 at 04:10
-
Thanks! I will keep it in mind! – Quan Zhou Jul 27 '18 at 04:26
1 Answers
3
__init__.py
is executed when imported on the first time. The latter imports in an application lifetime always loads from cache file.

Kavin Ruengprateepsang
- 315
- 2
- 6
-
1"The latter imports in an application lifetime always loads from cache" ...unless reloaded (for example, via `importlib.reload`. – Amadan Jul 27 '18 at 04:09