python之import不同文件下的文件

之前写python遇到一个情况,需要调用不同项目下的python文件,后面找到了解决方案

1
2
3
4
5
6
#比如要调用/path/to/application/app/folder/file.py

import sys
sys.path.insert(1,'/path/to/application/app/folder')

import file

就是讲需要添加的python文件加入到sys中,这样python就能默认调用了

参考链接:

https://stackoverflow.com/questions/4383571/importing-files-from-different-folder