问题:
How do I import a Python module given its relative path? 如何根据相对路径导入Python模块?
For example, if dirFoo contains Foo.py and dirBar , and dirBar contains Bar.py , how do I import Bar.py into Foo.py ? 例如,如果dirFoo包含Foo.py和dirBar ,并且dirBar包含Bar.py ,如何将Bar.py导入Foo.py ?
Here's a visual representation: 这是一个直观的表示:
dirFoo\
Foo.py
dirBar\
Bar.py
Foo wishes to include Bar , but restructuring the folder hierarchy is not an option. Foo希望包含Bar ,但重组文件夹层次结构不是一种选择。
解决方案:
参考一: https://stackoom.com/question/1Adp/从相对路径导入模块参考二: https://oldbug.net/q/1Adp/Import-a-module-from-a-relative-path
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/4331801