比如 我要运行 python /opt/a/b/c.py 然后需要在c运行时import b里面的其它module
那么我会在c中加入如下代码:
import sys, os
if __name__=='__main__':
____father_dir = os.path.split(os.path.realpath(__file__))[0].rpartition('/')[0]
____if father_dir not in sys.path:
________sys.path.insert(0, father_dir)
其中____表示站位
这样便可以import b里面的东西了, 但是许多文件都需要这个功能, 有什么比较优雅的方法吗?
那么我会在c中加入如下代码:
import sys, os
if __name__=='__main__':
____father_dir = os.path.split(os.path.realpath(__file__))[0].rpartition('/')[0]
____if father_dir not in sys.path:
________sys.path.insert(0, father_dir)
其中____表示站位
这样便可以import b里面的东西了, 但是许多文件都需要这个功能, 有什么比较优雅的方法吗?
