问题:
I want to import a function from another file in the same directory. 我想从同一目录中的另一个文件导入函数。
Sometimes it works for me with from .mymodule import myfunction
but sometimes I get a: 有时它与from .mymodule import myfunction
对我from .mymodule import myfunction
但有时我得到:
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. 这看起来完全是随机的。
Could someone explain to me what's the logic behind all this? 有人可以向我解释所有这些背后的逻辑是什么?
解决方案:
参考一: https://stackoom.com/question/19Flx/Python-中的相对导入
参考二: https://oldbug.net/q/19Flx/Relative-imports-in-Python-3