Skip to content Skip to sidebar Skip to footer

How To Resolve "valueerror: Attempted Relative Import Beyond Top-level Package"

I have the following problem with my project, help me please! Here is the structure of my package: /pkg /pkg/__init__.py /pkg/sub1/__init__.py /pkg/sub2/__init__.py /pkg/sub1/foo

Solution 1:

It seems to me, that without adding pkg to my PATH it is impossible to import modules from sub2 in sub1. Here is explanation why:

Relative imports use a module's name attribute to determine that module's position in the package hierarchy. 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.

Here is official python web site, where it is explained

Post a Comment for "How To Resolve "valueerror: Attempted Relative Import Beyond Top-level Package""