Relative import of python module from current working directory

In the current working directory , i have following structure

Project    __init__.py    -RestApi            __init__.py            app.py            query_generator    -testfolder            __init__.py            test1.py 

I want to call query_generator from test1.py , I tried calling

 from . RestApi.query_generator import * 

but getting following error

ImportError: attempted relative import with no known parent package 

This question might be duplicate of following Importing files from different folder , Python relative-import script two levels up, Attempted relative import with no known parent package . But I am not able to solve it for my current problem

Add Comment
2 Answer(s)

Try using below import:

from Project.RestApi.query_generator import * 
Add Comment

There are multiple ways to achieve this. you can add path till Project dir in your PYTHONPATH variable

export PYTHONPATH=$PYTHONPATH:<path_leading_to_Project>/Project 

Then inside test1.py you can import the query_generator module using:

from RestApi.query_generator import * 

Advantage of doing in such a way is if you execute your script from any working directory, it will work

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.