Important variables:
-
__name__
: Relative imports use a module's name attribute to determine that module's position in the package hierarchy. -
__package__
: Package hierarchy, in other words, the algorithm to resolve the module is based on the values of__name__
and__package__
variables. -
__file__
: Helps us debug by showing us the pathname of the file from which the module was loaded
In cases where where you recieve the error in question, __package__
will be None
and __name__
will be __main__
.
Why?
If the module's name does not contain any package information (e.g., it is set to
__main__
), then relative imports are resolved as if the module were a top-level module, regardless of where the module is actually located on the file system.