问题:
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参考二: Import a module from a relative path