Python 3中的相對導入 - Relative imports in Python 3

問題:

I want to import a function from another file in the same directory. 我想從同一目錄中的另外一個文件導入函數。 dom

Sometimes it works for me with from .mymodule import myfunction but sometimes I get a: 有時它與from .mymodule import myfunction對我from .mymodule import myfunction但有時我獲得: ide

SystemError: Parent module '' not loaded, cannot perform relative import

Sometimes it works with from mymodule import myfunction , but sometimes I also get a: 有時它與from mymodule import myfunction ,但有時我也獲得: 函數

SystemError: Parent module '' not loaded, cannot perform relative import

I don't understand the logic here, and I couldn't find any explanation. 我不瞭解這裏的邏輯,也找不到任何解釋。 This looks completely random. 這看起來徹底是隨機的。 this

Could someone explain to me what's the logic behind all this? 有人能夠向我解釋全部這些背後的邏輯是什麼? spa


解決方案:

參考一: https://stackoom.com/question/19Flx/Python-中的相對導入
參考二: https://oldbug.net/q/19Flx/Relative-imports-in-Python-3
相關文章
相關標籤/搜索