│ ├── __init__.py
│ ├── c.py
1 directory, 3 files
▶ tail -n +1 **/*.py
==> a/__init__.py <==
==> a/c.py <==
==> b.py <==
▶ python3 -m a.c
if __name__ == "__main__"
Module a.c has no dependencies on module a. So, why does Python run module a as if it were importing it
Importing parent.one will implicitly execute
parent/one/__init__.py. Subsequent imports of
To me this is surprising behavior. I didn't expect running a script without imports would cause the script itself to be imported and evaluated twice.
No, this isn't suprising. Think about this: whatever objects are exposed by package
a in your example is controlled by
a/__init__.py. The interpreter now has to evaluate
a/__init__.py to find out if
a.c exists at all. And in the process, the print statement is executed. Further, no module is imported twice. It is imported only once -- although you could technically write import statements multiple times.