I ran across a very specific case where I cannot explain myself, why the python import does what it does and I am hoping you can help me. The set up is as follows:
root/
├── main.py
└── pack/
├── __init__.py
├── db.py
└── plugins/
├── __init__.py
└── plug1/
├── __init__.py
└── db.py
All files in the tree above are empty except root/pack/plugins/plug1/__init__.py
which has the following content:
from pack.plugins.plug1 import db as plugin_db
from pack import db
and main.py
with the following content:
import pack.plugins.plug1.db as plug_db
import pack.db as pack_db
print(pack_db.__file__)
print(plug_db.__file__)
When running main.py
the output of the print statements is:
.../root/pack/db.py
.../root/pack/db.py
meaning the imported module is exactly the same.
My questions are:
- Why are the imported modules the same?
- Why does changing
from pack import db
tofrom pack import db as pack_db
resolves the issue?