python3 module built-in variables

print (dir ()) print module built-in variables

d:\PythonStudy\seven>python c10.py
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
'''
asdads
'''
print(__name__)                 //打印出完整的 命名空间
print(__package__)              //包
print(__doc__)                  //注释
print(__file__)                 //文件路径

python module executed out of the inlet and the built-in variables introduced module executed out values ​​are different.

Direct execution module inlet

print(__name__)
print(__package__)
print(__doc__)
print(__file__)
d:\PythonStudy\seven\t>python c9.py
__main__
None
None
c9.py

Introduction module execution

c9.py
print(__name__)
print(__package__)
print(__doc__)
print(__file__)
c10.py
import t.c9
d:\PythonStudy\seven>python c10.py
t.c9
t
None
d:\PythonStudy\seven\t\c9.py

To see if the variable function or a module, or a class of the module need only incoming dir () to

import sys
print(dir(sys))

__name__Magical

__name__If it is __main__then that he is a representative of entrance documents directly executed.

if __name__ == '__main__':
        print("这是入口")
print('这是模块')

As the two executable files can be printed out
as a module you can only print out "This is the module"

The executable file as a module to run

python -m c15.py // this is not enough

python -m seven.c15.py

The biggest difference is __package__whether there is, if an ordinary module that there must be a package, if no package, it is an executable file.

Published 65 original articles · won praise 3 · views 50000 +

Guess you like

Origin blog.csdn.net/web_orange/article/details/78124165