From PEP 328 :
Relative imports use the __name__ attribute to determine the position of the module in the package hierarchy. If the module name does not contain any information about the package (for example, it is set to " __main__ "), then relative imports are allowed as if the module was a top-level module, regardless of where the module is actually located in the file system.
Clarification: the __name__ attribute will usually be the path that the module has when importing it, for example. in foo / bar.py , if foo was a top-level package, __name__ would be 'foo.bar'. In the special case of the .py file that you use directly, __name__ is evaluated as '__main__' , which means that relative imports will not work.
source share