【发布时间】:2019-08-04 13:10:43
【问题描述】:
考虑以下几点:
a.py
foo = 1
b.py
bar = 2
c.py
import a
kik = 3
d.py
import a
import c
def main():
import b
main()
main()
- a.py 加载了多少次?
- b.py 加载了多少次?
更一般地说,我想知道 Python 如何处理导入的文件和函数/变量?
【问题讨论】:
-
realpython.com/absolute-vs-relative-python-imports
The first thing Python will do is look up the name abc in sys.modules. This is a cache of all modules that have been previously imported. -
你可以运行
strace -e trace=file python3 d.py来查看python正在访问哪些文件以及访问的顺序
标签: python