模块
#
# 使用模块
import sys
print('The command line arguments are:')
for i in sys.argv:
print(i)
print('\n\n the pythonpath is', sys.path, '\n')
print('\n')
import sys
当 Python 运行 import sys 这一语句时,它会开始寻找 sys 模块。
在这一案例中,由于其 是一个内置模块,因此 Python 知道应该在哪里找到
sys.argv
sys 模块中的 argv 变量通过使用点号予以指明,也就是 sys.argv 这样的形式。
它清晰地 表明了这一名称是 sys 模块的一部分。
这一处理方式的另一个优点是这个名称不会与你程序 中的其它任何一个 argv 变量冲突。
# from..import
语句
from math import sqrt
print("Square root of 16 is", sqrt(16))
# 模块的 __name__
def fun_dect():
if __name__ == '__main__':
print('this program is run by itself')
else:
print('this module is imported by another module')
fun_dect()
# 一个简单 demo
def func_sayhi():
print('sayhi')
__version__ = 0.1
## 1
import mymodule
mymodule.func_sayhi()
print('module version', mymodule.__version__)
## 2
from mymodule import func_sayhi, __version__
func_sayhi()
print('module version', __version__)