python中的__file__和__name__

测试:
import subprocess
print subprocess.__file__

运行结果:subprocess.pyc文件的绝对路径

变量__file__表示文件本身,输出的是一个绝对路径

*****************************************************************************************************************************************

#在test1/main.py中

def bar_1():
    print(__name__)

bar_1()

脚本本身执行的话

输出为:     __main__

被调用时执行则输出   脚本的名字

#在test2/bar_2.py中

from test1 import main
main.bar_1()

输出为:  test1.main

猜你喜欢

转载自blog.csdn.net/reasonyuanrobot/article/details/83897864