Python共有3种导入模块的方法
举例:现有文件夹class01,其下有Python file demo1,demo1.py有函数test01(),如何在另一个文件demo2.py中调用test01()?
调用某个模块下的内容
先导入对应文件/文件夹下的Python文件,才能调用其中的函数
1.单个功能引入
引入:import 模块名(文件夹名.文件名)单个功能引入
使用:模块名.函数
#class01/demo1.py
def test01():
print("hello,i'm from demo1.py")
def test02():
print("hello,i'm from demo1.py")
#demo2.py
import class01.demo1
class01.demo1.test01()
2.多个功能引入
引入:from 模块名 import 函数名,函数名
使用:根据函数名直接调用
#demo2.py
from class01.demo1 import test01,test02
test01()
test02()
3.全部功能引入
引入:from 模块名 import *
使用:根据函数名直接调用
#demo2.py
from class01.demo1 import *
test01()
test02()
本文介绍了Python中导入模块的三种方法:1) 使用`import`关键字完整导入;2) 使用`from...import`导入指定函数;3) 使用`from...import *`导入所有内容。以class01文件夹下的demo1.py为例,演示了如何在demo2.py中调用demo1.py的test01()函数。
1954





