1

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

0

2 Answers 2

1

Try using below import:

from Project.RestApi.query_generator import *
Sign up to request clarification or add additional context in comments.

10 Comments

from ProjectAware.RestApi.query_generator import * ModuleNotFoundError: No module named 'ProjectAware'
getting ModuleNotFoundError
Did you executing file like: python testfolder/test1.py ?
Yes I am doing that
Can you check your python path and verify that your CWD is present in python path after executing this file? Use sys module for this. Just print(sys.path)
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.