背景:当代码工程比较大时,经常会引用不同文件夹下面的文件,由于python和运行python代码平台出现路径引用格式的不同,会造成无法找到该文件。
解决办法1:
ROOT_DIR = os.path.abspath("../") # 定位执行文件的绝对路径,通过"../"的方式跳转到指定目录或主目录(根据情况修改)
sys.path.append(ROOT_DIR) # 添加到系统路径(关键部分)
from *** import ****
解决办法2:
# make sure that the cwd() is the location of the python script (so that every path makes sense)
os.chdir(os.path.dirname(os.path.abspath(__file__)))