模块与包

目录:

    模块与包

    系统常用模块

第一部分:模块与包

可以把模块想象成导入python以增强其功能的扩展。需要使用特殊的命令import导入模块,导入模块后,使用 ”模块.函数 “ 的格式调用这个模块的函数。下面举一个例子:

>>> import math
>>> math.floor(32.9)
32
>>> import sys
>>> sys.path
['', 'D:\Python35\Lib\idlelib', 'D:\Python35\python35.zip', 'D:\Python35\DLLs', 'D:\Python35\lib', 'D:\Python35', 'D:\Python35\lib\site-packages']
>>> 

 除了上面的导入方式,还有其他几种导入方式:

1 >>> import urllib    #导入urllib包
2 >>> from urllib.request import urlopen       #from 包名  导入模块名
3 >>> from urllib.reuqest import urlopen as urlo  #from 包名  导入模块名 as 别名 ,以后我们就可以使用别名替代模块名操作了
4 >>> from urllib import * #导入urllib.__all__列表中列出的所有模块,如果有的话,否则导入__init__.py文件中的内容。

 那么我们导入包的时候。python解释器做了哪些事情呢?

    1.产生了新的名称空间

    2.以新建的名称空间为全局名称空间,执行文件的代码

    3.拿到一个模块名称,指向模块名.py文件产生的名称空间,

那么,我们可能用到一个包中的很多模块,为了方便,能否就是用"from 包 import *"这样的导入语句呢?不建议这样使用,这样写代码虽然方便,不用加前缀,但缺点是很容易和当前文件的名称空间冲突,产生不好的后果。

模块中一些常用方法:

   1).  __all__

 1 #MyModule.py
 2 
 3 __all__=['fun1',]
 4 
 5 def fun1():
 6     print('MyModule-->fun1')
 7 
 8 def fun2():
 9     print('MyModule-->fun2')
10 
11 
12 #test.py
13 from MyModule import *
14 fun1()    # 调用MyModule的fun1
15 fun2()    #调用MyModule的fun2

    可以看到我们只能在当前的名称空间中只能找到fun1,不能找到fun2,__all__在模块中指定了使用“from 模块名 import *"是都导入哪些函数,它是一个列表。

     2).  __file__

1 #test.py
2 filename=__file__
3 print(filename)
4 
5 结果:
6 D:/PycharmProjects/untitled11/test.py

 __file__ 会返回当前文件的文件名称

3). __name__

1 #MyModule.py
2 Version=1.03 print(__name__)
4. 4 #test.py 5 import MyModule 6 7 #结果: 8 MyModule #通过import导入模块是,打印模块的__name__得到模块的名称
1 #test.py
2 
3 print(__name__)
4 
5 结果:
6 __main__

在当前的名称空间打印__name__会返回__main__。通过上面的例子,我们就会发现可以通过__name__来判断当前模块作为主模块运行还是被导入

1 def main():
2     print('now running the file %s' % __file__)
3 if __name__=='__main__':
4     main()
5 结果:now running the file D:/PycharmProjects/untitled11/test.py

 很多功能相似的模块放在一起,在添加一些__init__.py文件,就构成了包。下面是一个包的例子:

1 drawing_board
2 │   ├── hard_brush
3 │   │   ├── ball_pen.py
4 │   │   └── __init__.py
5 │   ├── __init__.py
6 │   └── soft_brush
7 │       ├── __init__.py
8 │       ├── pen.py
9 │       └── test.py

 第二部分:系统常用模块

    1). sys

    sys模块能够让你访问与python解释器紧密联系的变量和函数。

 1 #sys module
 2 >>> import sys    #导入sys模块
 3 >>> sys.path      #打印path变量
 4 ['', 'D:\Python35\Lib\idlelib', 'D:\Python35\python35.zip', 'D:\Python35\DLLs', 'D:\Python35\lib', 'D:\Python35', 'D:\Python35\lib\site-packages']
 5 >>> sys.argv
 6 ['']
 7 >>> sys.getwindowsversion    
 8 <built-in function getwindowsversion>
 9 >>> sys.getwindowsversion()
10 sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')
11 >>> sys.version   #获取python类型
12 '3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]'
13 >>> sys.exit() #推出python解释器

2).os

 os 模块提供了访问多个操作系统服务的功能。os 及子模块 os.path还包括一些用于检查/构造/删除目录和文件的函数,以及一些处理路径的函数。功能强大。

 1 >>>import os
 2 >>> os.name
 3 'nt'
 4 >>> os.getcwd()
 5 'D:\Python35'
 6 >>> os.sep
 7 '\'
 8 >>> os.linesep
 9 '
'
10 >>>
1 >>> os.path.exists('libs')
2 True
3 >>> os.listdir()
4 ['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python35.dll', 'pythonw.exe', 'README.txt', 'Scripts', 'tcl', 'Tools', 'vcruntime140.dll']
5 >>> os.path.exists('libs')
6 True
7 >>> os.path.dirname(os.getcwd())
8 'D:\'
原文地址:https://www.cnblogs.com/mingxiazhichan/p/6935716.html