知识点回顾:
- 导入官方库的三种手段
- 导入自定义库/模块的方式
- 导入库/模块的核心逻辑:找到根目录(python解释器的目录和终端的目录不一致)
作业:自己新建几个不同路径文件尝试下如何导入
from lib.utils import add
from lib.sublib.helper import helper_func
print(add(2, 3))
print(helper_func(3))
def add(a, b):
return a + b
from ..utils import add
def helper_func(x):
return add(x, x)
import sys
sys.path.append('../')
from lib.utils import add
def test_add():
assert add(1, 2) == 3, "测试失败"
test_add()
print("测试通过!")