從相對路徑導入模塊 - Import a module from a relative path

問題:

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.pydirBar ,而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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章