With the following directory structure
root/
├ core/
│ └ base.py
│
├ sub/
│ └ subA.py
Try to call base.py from subA.py.
sub.py
from ..core.base import *
Then the following error occurs
Exception Value: attempted relative import beyond top-level package
The cause is that there is a specification that "Python uses the current directory of the file to be executed as the root directory of the highest level", and it occurs because it can not be moved to the upper directory.
You can set the environment variable PYTHONPATH.
export PYTHONPATH=/root
And modify the code as follows
sub.py
from core.base import *
Now that `` `/ rootis added to the package search path when python is executed, you can search for directories under
/root```.
Reference: https://docs.python.org/ja/3/using/cmdline.html#envvar-PYTHONPATH
Recommended Posts