python2.7 - python解释器import和脚本的执行影响了结果

查看:82
本文介绍了python2.7 - python解释器import和脚本的执行影响了结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

#test_name0.py

import test_name1
test_name1.test()

#test_name1.py

def test():
    print __name__
    
print __name__

如果单独运行test_name0.py的脚本,或者单独在解释器中import test_name0得到的结果是

test_name1
test_name1

但是如果在运行了test_name0.py的脚本后再在解释器中import test_name0,或者在解释器中import test_name0后再运行test_name0.py的脚本,得到的结果是

test_name1

我想知道为什么会出现这种情况,我使用的IDE是spyder(python2.7)

解决方案

python模块有缓存,import一次后再import,模块顶级作用域的代码不会再执行

这篇关于python2.7 - python解释器import和脚本的执行影响了结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆