如同,utils.py以相对路径读取文件可以正常运行,但drug_standarlization_offlint_group.py调用utils.py则报错访问不到该文件。
解决方法,uitls.py将相对路径改为绝对路径。由于代码需要线上部署,故绝对路径不能写死,通过os.path函数对绝对路径进行拼接,代码如下:
import os
import json
class NameUtils():
def __init__(self):
basepath = os.path.abspath(__file__)
folder = os.path.dirname(basepath)
exec_path = folder.replace('name_match', 'spec_structure')
print(basepath)
print(folder)
data = json.loads(open(exec_path+'/dict/test.json', "r", encoding="utf-8").read())
print(data)
if __name__ == '__main__':
NameUtils()