简体   繁体   中英

Attribute not added to module after importing

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM