模块:
顶层文件(也是模块文件)
模块文件
模块在物理形式上表现为.py结尾的代码文件
Python中,模块本身也是对象,所以模块也有变量,函数,类
在一个模块的顶层定义的所有变量都在被导入时成为了被导入模块的属性
查看标准库模块:
help(‘modules‘)
顶层文件:包含了程序的主要控制流程
模块文件:为顶层文件或其他模块提供各种功能组件
import module [,module1[,module2]]语句 导入模块
导入指定的整个模块,包括生成一个以模块名命名的名称空间
导入模块别名(有别名以后不能再使用原名,防止重复的名字空间):
import module as module_alias
from-import语句(负面作用:会导致属性覆盖)
常用于只导入指定模块的部分属性至当前名称空间
from module import name1[,name2[,name3]]
import和from-import是赋值语句
import和from是可执行语句,类似于def,因此他们嵌套再if测试中,出现def中等等
import和from都是隐形的赋值语句
如果本程序中有声明变量a = 10 ,导入的模块中也有变量a = 20,那么模块中的变量覆盖原程序的变量,此时a = 20
import语句导入模块三个步骤:
1.找到模块文件:在指定的路径下搜索模块文件(依次查找,查到就停止)
搜索的路径:程序的主目录,PythonPath目录(如果设置了此变量),标准链接库目录,任何.pth文件的内容(如果存在.pth文件)
In [9]: import sys In [11]: print (sys.path) [‘‘, ‘/usr/local/python361/bin‘, ‘/usr/local/python361/lib/python36.zip‘, ‘/usr/local/python361/lib/python3.6‘, ‘/usr/local/python361/lib/python3.6/lib-dynload‘, ‘/usr/local/python361/lib/python3.6/site-packages‘, ‘/usr/local/python361/lib/python3.6/site-packages/ipython-6.1.0-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/pexpect-4.2.1-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/Pygments-2.2.0-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/prompt_toolkit-1.0.14-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/traitlets-4.3.2-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/simplegeneric-0.8.1-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/pickleshare-0.7.4-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/decorator-4.0.11-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/jedi-0.10.2-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/ptyprocess-0.5.1-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/wcwidth-0.1.7-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/six-1.10.0-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/ipython_genutils-0.2.0-py3.6.egg‘, ‘/usr/local/python361/lib/python3.6/site-packages/ipython-6.1.0-py3.6.egg/IPython/extensions‘, ‘/root/.ipython‘]
例,新建一个模块,让程序能够搜索到该模块文件
[[email protected] ~]# mkdir -pv /opt/python361/ mkdir: created directory `/opt/python361/‘ [[email protected] ~]# touch /opt/python361/mymod.py [[email protected] ~]# vim /opt/python361/mymod.py
#! /usr/bin/python36 # a = 10 def printInfo(): print a + 30 class Mym(): im = ‘hello Mym‘ def __init__(self,x): self.name = x def printName(self): print self.im, self.name
直接导入会搜索不到
In [13]: import mymod --------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-13-7215bcbec8da> in <module>() ----> 1 import mymod ModuleNotFoundError: No module named ‘mymod‘
添加sys.path路径
In [14]: sys.path.append(‘/opt/python361‘) In [15]: import mymod In [16]: In [17]: print (mymod.a) 10
2.编译成字节码:文件导入时就会编译,因此,顶层文件的.pyc字节码文件在内部使用后会被丢弃,只有被导入的文件才会留下.pyc文件
3.执行模块的代码来创建其所定义的对象:执行里面的顶层文件
如果一个模块被导入,则__name__属性保存的是自己的模块名称
如果不是被导入,而是自己执行,则__name__属性保存的是__main__
[[email protected] python361]# cat /opt/python361/mymod.py #! /usr/bin/python36 # a = 10 def printInfo(): print a + 30 class Mym(): im = ‘hello Mym‘ def __init__(self,x): self.name = x def printName(self): print self.im, self.name if __name__ == ‘__main__‘: ##自我测试测试代码 printInfo() ins1 = Mym(‘jerry‘) print ins1.im print ins1.name
In [27]: mymod.__name__ Out[27]: ‘mymod‘
[[email protected] python361]# python mymod.py 40 hello Mym jerry
注意:
第一次导入才会执行三个步骤,重复导入没有意义,只是在内存中提取已加载的模块。
reload()可用于重新加载模块