問題:
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