一、.py文件可以看做一个模块,模块类似其他语言中封装的类库
模块分类:
内置模块
自定义模块
第三方模块(需要安装才能使用)
我想要使用cal.py中定义的函数,可以这样做
cal.py源代码:
#!/usr/bin/python
#coding:utf-8
def add( a, b ):
return a + bimport_test.py要使用add函数:
#!/usr/bin/python #coding:utf-8 import cal print cal.add( 10, 20 )
二,内置属性__name__
cal.py
#!/usr/bin/python
#coding:utf-8
def add( a, b ):
return a + b
print __name__ghostwu@ghostwu:~/python$ python cal.py __main__ ghostwu@ghostwu:~/python$ python import_test.py cal 30 ghostwu@ghostwu:~/python$
当执行cal.py本身时,__name__被解释成__main__, 通过模块引入方式执行时,别解释成文件名,这个特性有什么用?
#!/usr/bin/python
#coding:utf-8
def add( a, b ):
return a + b
if __name__ == '__main__':
print add( 100, 200 )ghostwu@ghostwu:~/python$ python import_test.py 30 ghostwu@ghostwu:~/python$ python cal.py 300
这就是__name__的一个小作用,当一个模块(.py文件)被别的文件调用时,一般是想调用它里面定义的函数或者方法,我们可以通过__name__让外部模块调用方式,不会执行到类似print add( 100, 200 )这样的
具体业务代码。只用到里面的函数或者方法定义。
三、模块加载顺序:
当前目录如果有同名的系统模块,那么当前目录的模块会被import,系统模块会被忽略,如:
ghostwu@ghostwu:~/python/module$ ls
import_test.py string.py
ghostwu@ghostwu:~/python/module$ cat string.py
#!/usr/bin/python
#coding:utf-8
def add( a, b ):
return a + b
ghostwu@ghostwu:~/python/module$ cat import_test.py
#!/usr/bin/python
#coding:utf-8
import string
str = 'ghostwu'
print string.capitalize( str )
ghostwu@ghostwu:~/python/module$ python import_test.py
Traceback (most recent call last):
File "import_test.py", line 8, in <module>
print string.capitalize( str )
AttributeError: 'module' object has no attribute 'capitalize'
ghostwu@ghostwu:~/python/module$ ls -a
. .. import_test.py string.py string.pyc在当前目录下,定义了一个同名的string模块( 指的是与系统的string模块同名 ),由于执行的时候,当前目录的模块被import了,所以识别不了系统string模块的方法capttalize.
只要删除目录下的string.py string.pyc,就能正常import系统的模块
ghostwu@ghostwu:~/python/module$ ls -a . .. import_test.py string.py string.pyc ghostwu@ghostwu:~/python/module$ rm string.py string.pyc ghostwu@ghostwu:~/python/module$ ls -a . .. import_test.py ghostwu@ghostwu:~/python/module$ python import_test.py Ghostwu
四,当一个目录下有__init__.py文件,就可以当做一个包来用
ghostwu@ghostwu:~/python/package_test$ pwd
/home/ghostwu/python/package_test
ghostwu@ghostwu:~/python/package_test$ ls -a
. .. calc.py calc.pyc fab.py fab.pyc __init__.py __init__.pyc
ghostwu@ghostwu:~/python/package_test$ cat calc.py
#!/usr/bin/python
#coding:utf-8
def add( a, b ):
return a + b
def sbb( a, b ):
return a - b
def mul( a, b ):
return a * b
def div( a, b ):
return a / b
oper = { '+' : add, '-' : sbb, '*' : mul, '/' : div }
def mySwitch( x, o, y ):
return oper.get( o )( x, y )
ghostwu@ghostwu:~/python/package_test$ cat fab.py
#!/usr/bin/python
#coding:utf-8
def fab( n ):
if n == 0:
return 1
else:
return n * fab( n - 1)在test.py中,引用包中的模块
ghostwu@ghostwu:~/python/package_test$ cd .. ghostwu@ghostwu:~/python$ ls -a . .. module package_test test.py tmp ghostwu@ghostwu:~/python$ cat test.py #!/usr/bin/python #coding:utf-8 import package_test.calc import package_test.fab print package_test.calc.add( 10, 20 ) print package_test.fab.fab( 6 ) ghostwu@ghostwu:~/python$ python test.py 30 720 ghostwu@ghostwu:~/python$
导入模块的另外两种方式: 别名与直接导入方法
ghostwu@ghostwu:~/python/package_test$ ls calc.py calc.pyc fab.py fab.pyc __init__.py __init__.pyc ghostwu@ghostwu:~/python/package_test$ python Python 2.7.12 (default, Dec 4 2017, 14:50:18) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import calc >>> calc.add( 10, 20 ) 30 >>> import calc as c >>> c.add( 100, 200 ) 300 >>> from calc import add >>> add( 1, 2 ) 3