qaiser
qaiser

Reputation: 2868

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

Upvotes: 1

Views: 687

Answers (2)

Rachit Tayal
Rachit Tayal

Reputation: 1292

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

Upvotes: 1

Astik Gabani
Astik Gabani

Reputation: 605

Try using below import:

from Project.RestApi.query_generator import *

Upvotes: 1

Related Questions