1、python中用import关键字来引入模块
import module1,module2,......
例如:导入一个random模块,使用randint方法产生一个1到5之间的随机数。
>>> import random
>>> random.randint(1,5)
4
>>> random.randint(1,5)
4
>>> random.randint(1,5)
4
>>> random.randint(1,5)
2
>>> random.randint(1,5)
3
>>>
2、自定义模块
例如test.py为我们自定义的模块,在main.py里面调用此模块
[root@mail python]# cat test1.py
#!/bin/env
#coding:utf-8
def example():
print("hahahha")
[root@mail python]# cat main.py
#!/bin/env
#coding:utf-8
import test1 #只需要在将xxx.py中的xxx导入
test1.example()
这样就可以正常执行
[root@mail python]# python main.py
hahahha
如果写test1()的开发人员想在main()函数调用之前测试自己的代码对不对
可以这样做
#!/bin/env
#coding:utf-8
def example():
print("hahahha")
example()
[root@mail python]# python test1.py
hahahha
但是main()函数在调用此模块时此模块会被执行两次
[root@mail python]# python main.py
hahahha
hahahha
解决办法:使用__name__
3、__name__
的使用
我们在模块函数里面输出__name__
[root@mail python]# cat test1.py
#!/bin/env
#coding:utf-8
def example():
print("hahahha")
#example()
print(__name__)
输出结果为__main__
[root@mail python]# python test1.py
__main__
在调用模块的函数里面输出__name__
[root@mail python]# cat main.py
#!/bin/env
#coding:utf-8
import test1
#test1.example()
print(__name__)
输出结果为模块名
[root@mail python]# python main.py
test1
__main__
所以对于之前的问题我们可以在主函数里面加入以下判断
(1)在mian.py里面添加判断
import test1
#test1.example()
if __name__ == "test1":
test1.example()
test.py不变
def example():
print("hahahha")
example()
执行结果
[root@mail python]# python main.py
hahahha
(2)在test.py里面加入判断,main.py不变
test.py文件
def example():
print("hahahha")
if __name__ == "__main__":
example()
main.py文件
import test1
test1.example()
执行结果
[root@mail python]# python main.py
hahahha
4、import导入路径的问题
上面讲了,我们导入模块,要调用模块中的方法时要使用模块.方法
来调用,直接调用方法会报错
>>> import random
>>> random.randint(1,3)
3
>>> rrandint(1,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'rrandint' is not defined
>>>
如果想直接使用某方法,则需要from 模块名 import 方法名
>>> from random import randint
>>> randint(1,3)
1
(1)from random import * :表示导入random模块里的所有方法
(2)from 模块 import _all_:
自定义一个模块test2.py
在main.py里面调用模块
[root@localhost python]# cat test2.py
#!/bin/env
#coding:utf-8
class Test:
def test(self):
print("------test------")
def test1():
print("-------test1---------")
def test2():
print("--------test2---------")
[root@localhost python]# cat main.py
#!/bin/env
#coding:utf-8
from test2 import *
test = Test()
test.test()
test1()
test2()
[root@localhost python]# python main.py
------test------
-------test1---------
--------test2---------
运行完成后会在当前目录下产生一个test2.pyc的文件,这是一个编译过的文件,下次运行此程序时可以不用编译,直接执行。
[root@localhost python]# ls
chuandiyichang.py main1.py test1.py test2.py zidingyi2.py
mail.py main.py test1.pyc test2.pyc zidingyi.py
接下来我们在模块文件test2.py中定义__all__
__all__ = ["Test","test1"]
class Test:
def test(self):
print("------test------")
def test1():
print("-------test1---------")
def test2():
print("--------test2---------")
再在main.py中调用执行时会发生这样的报错
[root@localhost python]# python main.py
------test------
-------test1---------
Traceback (most recent call last):
File "main.py", line 9, in <module>
test2()
NameError: name 'test2' is not defined
所以__all__定义的内容就是from 模块 import *
中*
所对应的内容
如果我们还想调用test2(),则再专门调用一次即可
from test2 import *
from test2 import test2
test = Test()
test.test()
test1()
test2()
[root@localhost python]# python main.py
------test------
-------test1---------
--------test2---------