什么是模块?
模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py。模块可以被使用该模块中的函数等功能。这也是别的程序引入,以使用 python 标准库的方法。
前面使用过的模块?
import keyword 关键字模块
import random 随机数模块
import time 时间模块
import math 数学模块
1、模块-导入
a、导入内置模块
import time
print(time.time())
具体用法
Import time
date = time.strftime(‘%Y-%m-%d %H:%M:%S’)
print(date)
运行效果: 2020-06-18 18:12:43
b、在模块2中导入模块1的函数名
方式一:
import test1
test1.foo()
test1.bar()
方式二:
导入单个函数
Form test1 import foo
Foo()
导入多个函数
form test1 import foo,bar
foo()
bar()
导入某个模块里的所有函数(不建议使用,需要哪个导入哪个)
from test1 import *
foo()
bar()
给函数起别名as
导入单个函数
Form test1 import foo as ooo
ooo()
2、包导入
导入指定模块
Import demo1.test1 as aaa
aaa.bar()
aaa.foo()
导入多个模块
import demo1.test1, demo1.test4
demo1.test1.foo()
demo1.test4.fun1()
导入包中模块的某个函数
from demo1.test1 import bar
bar()
导入包中模块的多个函数
from demo1.test1 import bar,foo()
bar()
foo()
导入某个包中某个模块的所有函数
from demo1.test1 import *
bar()
foo()