Python calls the module in the parallel package by obtaining the absolute path of the current file. This method can prevent the problem that the package cannot be called when the package is not in the pythonpath path.

sys.path.append(os.path.dirname(os.path.dirname(os.path.adspath(__file__))))
#__file__表示获取当前文件的文件名
#os.path.adspath(__file__)表示获取当前文件的绝对路径
#os.path.dirname(os.path.adspath(__file__))表示获取当前文件的文件夹的绝对路径
#os.path.dirname(os.path.dirname(os.path.adspath(__file__)))表示获取当前文件夹的上一级目录的绝对路径
#sys.path.append(os.path.dirname(os.path.dirname(os.path.adspath(__file__))))表示将前文件夹的上一级目录的绝对路径添加到pythonpath中,该方法添加的路径在程序关闭后失效
#该方法可防止别人将代码拷贝到其他电脑后,未将该代码放在pythonpath的路径中,造成调用失败的现象

Guess you like

Origin blog.csdn.net/weixin_44123630/article/details/111811415