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