Relative imports in Python 3

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:

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

Sometimes it works with from mymodule import myfunction, but sometimes I also get a:

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?

2
24

Leave a Comment