Attribute not added to module after importing

2020-04-17 05:33发布

问题:

I've made the following lab:

vagrant@ubuntu-xenial:~/test$ tree
.
├── pack1
│   ├── __init__.py
│   ├── mod1.py
│   └── pack2
│       ├── __init__.py
│       ├── mod2.py
│       └── mod3.py
└── test.py

2 directories, 6 files
vagrant@ubuntu-xenial:~/test$ tail -n +1 *.py */*.py */*/*.py
==> test.py <==
#!/usr/bin/python
import pack1.pack2.mod2
print(__file__)
try:
    print(pack1.__dict__['pack2'])
except Exception as error:
    print("MISSING PACK2")

==> pack1/__init__.py <==

==> pack1/mod1.py <==

==> pack1/pack2/__init__.py <==
import pack1.pack2.mod2

==> pack1/pack2/mod2.py <==
import pack1.pack2.mod3
print(__file__)
try:
    print(pack1.__dict__['pack2'])
except Exception as error:
    print("MISSING PACK2")

==> pack1/pack2/mod3.py <==
vagrant@ubuntu-xenial:~/test$ ./test.py
/home/vagrant/test/pack1/pack2/mod2.py
MISSING PACK2
./test.py
<module 'pack1.pack2' from '/home/vagrant/test/pack1/pack2/__init__.py'>
vagrant@ubuntu-xenial:~/test$

pack2 is not an attribute of pack1 within pack1/pack2/mod2.py after importing pack1.pack2.mod3.

It works, however, when the pack1/pack2/__init__.py does not import pack pack1.pack2.mod2.

Why is this?

回答1:

A module is added as an attribute of its package when its import finishes. (Note that this can interfere with circular relative imports.) Here, pack2 immediately imports mod2, so (regardless of further completed imports like that of mod3) it doesn’t appear in the pack1 dictionary during mod2’s execution.