I have a python script that wants to import another .py
file in a directory that is on the same level of its parent directory, the file system like the following
.
├── __init__.py
├── obslib
│ ├── __init__.py
│ └── jet
│ ├── __init__.py
│ ├── fakepdf.py
│ ├── fakepdf.pyc
│ ├── jet_mellin.py
│ ├── reader.py
│ ├── residuals.py
│ ├── theory.py
│ └── theory.pyc
├── readme.md
└── tools
├── README.md
├── __init__.py
├── __init__.pyc
├── bar.py
├── config.py
├── inputmod.py
├── multiproc.py
├── parallel.py
├── randomstr.py
├── reader.py
├── residuals.py
├── storage.py
└── tools.py
File jet_mellin.py
in directory jet
wants to import, say, class BAR
in bar.py
in directory tools
, it should be fine to just write
from tools.bar import BAR
However, I get error
ImportError: No module named tools.bar
Then I tried
from ..tools import bar
and get
ValueError: Attempted relative import in non-package
The only way I could make it work is following
import sys
sys.path.insert(0, '../../tools/')
from bar import BAR
But this would give rise to ambiguous situation if I want to have another script also called bar.py
in some other directory. So I would like to be able to specify the path for an import like in from ..tools import bar
. Could you please help me with this? Thanks in advance!