1、模块:
Python中一个.py文件就是一个模块
能避免函数名、变量名重名,增加代码的可读性
2、包:
新建package
每一个新建的包里都有一个__init__.py文件,每个模块都会执行一遍init里的代码。一般不建议删掉
往往是包里面存放着模块
二、导入
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()