Python BASE_DIR介绍
#我是bin.py文件 from my_module import cal #假设my_module这个包不与bin.py文件同级或者下级的话,这里调用就会出错,找不到my_module这个包,所以怎么解决呢 import os #定义BASE_DIR,再把BASE_DIR加入到sys.path里 #os.path.abspath是绝对路径,__file__是bin.py,根据绝对路径往上一级路径就能找到my_module的路径了 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) print(BASE_DIR) print(__file__)