'How to get rid of PYTHONPATH="./:${PYTHONPATH}" while running a script which uses relative imports
I have a directory as:
BASEDIR/
-> codes/
-> test.py
-> models/
-> __init__.py
-> module1.py
-> utils/
-> __init__.py
-> chind_module.py
-> helpers/
-> __init__.py
-> submodule.py
I am inside the BASEDIR and run the code inside test.py as: python ./codes/test.py and it gives me error as:
Traceback (most recent call last):
File "./codes/test.py", line 3, in <module>
from models import create_model
File "/home/shady/Desktop/Dresma/LPTN/codes/models/__init__.py", line 4, in <module>
from codes.utils import get_root_logger, scandir
ModuleNotFoundError: No module named 'codes'
Where code inside test.py is as:
from codes.models import create_model
from codes.utils.options import *
def main():
return True
if __name__ == '__main__':
main()
Digging inside codes/models/__init__.py, I got that it has relative import as from codes.helpers import *
But when I did PYTHONPATH="./:${PYTHONPATH}" python test.py, it worked fine.
How could I run the program without using PYTHONPATH="./:${PYTHONPATH}" ?
I tried the following but nothing worked
from models import create_model
from utils.options import *
from .models import create_model
from .utils.options import *
from ..models import create_model
from ..utils.options import *
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
