Although this is a topic covered in articles in various places, there are some parts that were difficult to understand, so I will organize them.
When does it occur?

It is as written, but I will organize it.
-
attempt relative import
As the name suggests, this occurs when performing an import using a relative path.
-
beyond top-level package
You cannot go beyond the 'top-level of the package'.
It is a little difficult to understand what it means to 'go beyond the top-level of the package'. This means 'you cannot cross the top-level of the package'. It does not mean 'you cannot go above the top-level of the package'. In other words,
\
├ top_module.py
├ second/
│ ├ third/
│ │ └ third_module.py
│ └ second_module.py
└ second-2/
└ second-2_module.pyWhen you try to import from third_module.py in a structure like this,
from .. import second_module # -> OK from ... import top_module # -> NG from ...second-2 import second-2_module # -> NG from .... import some_module # -> もちろんNG
That is the reason.
Points to note when importing within a module during module development
It is obvious, but when developing your own module, you must not pollute the external namespace. In other words,
\
├ package/
│ ├ lib/
│ │ └ module.py
│ └ conf/
│ └ config.py
└ conf/
└ config.pyWhen you have a structure like this, and you assume the package directory is the root within lib/module.py,
from conf import config
If you specify an absolute path like this, it will load a different file (in this case, conf/config.py directly under the root) depending on the location from which it is loaded or the environment state of the extended PYTHONPATH. To solve this, you need to load it as a relative path within the module. In other words,
from ..conf import config
it becomes this.
When developing with an IDE (Example of Visual Studio Code)
By the way, where the 'root directory' is depends on the execution environment. It is easy to understand that when a command is executed, it is the location where the command was executed. So, what happens when you are developing in an IDE and the path is resolved by the IDE's functions?
Basically, the root directory (workspace) where the project is opened is judged as the root. Therefore, depending on where you open it, the path may or may not be resolved. When creating a project, it is desirable to decide which directory to use as the root for development and resolve the path. Also, in the case of VSCode, you can load the .env file in the project's root directory to perform environment variable expansion, etc.You can also load it by using this to extend the PYTHONPATH. However, when doing this, the namespace within the module will not be unified, so you need to be careful as it may cause unexpected conflicts. For example, in the above example, if you extend it with PYTHONPATH="package:$PYTHONPATH", the conf module will conflict, so depending on the target, the import may fail.
Also, as mentioned above, since the workspace is judged as the root in VSCode, if you open the package folder in the above example as a workspace, the following in lib/module.py
from ..conf import config
will cause an 'attempted relative import beyond top-level package' error. Therefore, when developing a package, you should design the repository so that the workspace is at least one directory above the package root directory.