项目目录结构为:
before
├── main.py
└── test_code
├── init.py
├── test_cod.py
├── bb.py
└──cc.py
├── init.py
└── demo.py
test_cod.py
# from before.test_code import cc
import cc
import bb
print("this is test_cod")
bb.py
import test_cod
print("this is bb")
cc.py
import test_cod
print("this is cc")
运行test_cod.py,结果如下
this is bb
this is test_cod
this is cc
this is test_cod
|-import cc (已执行,不在执行)
|-import bb-|-import test_cod(已执行,不在执行)
| |-print("this is bb") 第一次打印
|-import test_cod-|-("this is test_cod") 第二次打印
|-print("this is cc") 第三次打印
|-import cc-|
test_cod-|-import bb--(已执行,不在执行)--------|---import test_cod
|-print("this is test_cod") 第四次打印 |---print("this is bb")
test_cod.py里代码的顺序是import cc再import bb,然后print(“this is test_cod”),
先import cc(即执行cc.py,第一次import test_cod (执行test_cod.py(这次是从import bb(第二次import test_cod,这次不会import,直接打印"this is bb",跳出)开始执行,然后打印"this is test_cod"),然后打印"this is cc"),因为bb已经被导入过一次,不在执行import bb,最后执行print(“this is test_cod”)。