Why do Python imports behave like this

The German Python forum

Post Mon Feb 18, 2019 17:01

@ _blackjack_:
hello, I also have a question of understanding:
Modules are executed the first time they are imported. Instruction after instruction. If one of these instructions is an `` import '', then the execution is interrupted at that point, similar to a function call, and the imported module is executed, provided that this is the first import from the module.
how do the outputs come about (when executing main.py)? Processing (execution of main.py):
main.py: import sub (first import) => sub.py: import main (first import) => main.py: print ("main.py") => main.py: def mainFunc () [does not produce any output , since it is a procedure - no return] => sub.py: print ("sub.py") => sub.py: def subFunc () [no output, there procedure - no return]
where does the second 'main.py' come from in the third position?

It would make sense to me:
first process the instructions from main.py, then process the import instruction:
main.py: print ("main.py") => main.py: def mainFunc () [does not produce any output because it is a procedure - no return] => main.py: import sub (first import) => sub.py: print ("sub.py") => sub.py: def subFunc () [no output, as procedure - no return] => sub.py: import main (first import) => main.py: import sub (second import [no processing / output, as it is already integrated]) => main.py: print ("main.py") => main.py: def mainFunc () [does not produce any output because it is a procedure - no return]
=> then I would have as output:
main.py
sub.py
main.py

it's the "little" things that make me despair or confuse my understanding.

Edit:
aha, you made an addition. Well, if it exists twice then I also know where the second "main.py" comes from, once from main () and once from main.py, right?
who can read has a clear advantage
there are no problems, only solutions!
Education is the joy of myself!