Import module
- Module is a
py
file - File is too large, too much accumulation of code, use a custom module to split into multiple files multiple lines of code, making the code more rigorous and clear
Introducing whole module
import demo
print(demo.a) # 变量
print(demo.b)
demo.func() # 函数
Introducing a single function
from demo import a # 导入a变量
from demo import b
print(a)
print(b)
from demo import a,b,func # 导入多个功能
print(a)
print(b)
func()
from demo import * # 导入所有demo中多少有功能
print(a)
print(b)
func()
from demo import a as abc,b as bcd # 导入功能,并重命名
print(abc,bcd)
Import module summary
from module import variables
import module
Module. The value of the variable access variables
import 模块名
from 模块名 import 变量名
from 模块名 import 变量1,变量2
from 模块名 import 变量1 as 新名1,变量2 as 新名2
from 模块名 import *
Introducing the package
Either import module or pack, you must make sure the path modules and packages to be introduced in the sys.path
list
import sys
sys.path.append('F:\python自动化27期\day8\模块和包\dir')
from pack.api import policy
print(policy.name)
导入包
from 包.包.包 import 模块
模块.xxx直接获取值
import 包.包.模块
包.包.模块,xxx获取值