centos和pycharm中取绝对路径的差别
pycharm中取执行文件绝对路劲并向上翻两次,得到项目目录
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os import sys project_path = os.path.dirname(os.path.dirname(__file__)) sys.path.append(project_path) #D:/sylar/s15/my_project from core import main if __name__ == '__main__': main.home()
centos7中的到执行文件的绝对路径并向上翻两层,的到项目目录
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import os import sys start_path=os.path.abspath('start.py') #取到start.py的绝对路径 bin_path=os.path.dirname(start_path) #连续向上翻两次 project_path=os.path.dirname(bin_path) # sys.path.append(project_path) print("hello") print(sys.path) print(os.path.abspath(project_path))
总结:
centos7中通过__file__方法得不到文件的绝对路径,只能通过os.path.abspath()得到绝对路径再向上翻