Make it easy to import the modules in parent directories
If you want to solve ValueError: attempted relative import beyond top-level package
, DotDot may be helpful for you.
If it can be installed and imported successfully, it should work correctly.
Install via pip:
pip install dotdot
Consider a common project structure as following:
FooProject
├── foo
│ ├── script.py
│ └── util.py
└── util.py
Now, script.py
can import those util
s by importing dotdot
first. Like this:
import dotdot
import foo.util
import util
Moreover, you can import the modules in grandparent or even great grandparent directory by importing dotdotdot
or dotdotdotdot
first.
DotDot does this magic by changing sys.path[0]
in import time.