I have two files in the following tree structure
├── __init__.py
├── pkg
│ ├── __init__.py
│ └── child.py
└── top.py
The top.py file looks like this
variable = "I live in the top directory"
The child.py looks like this
from ..top import variable
if __name__ == '__main__':
print variable
If I try to run the file through python pkg/child.py
I get an error saying: ValueError: Attempted relative import in non-package
. If run it from the pkg
directory as python child.py
I get the same error.
Reading from the answers provided in this answer:
How to do relative imports in Python? I tried doing python -m pkg.child
from the parent directory. Then I get the error: ValueError: Attempted relative import beyond toplevel package
.
So I'm starting to run out of ideas. How should you do it?