1

I know many people asked this question but I still can't figure this out.

My Project setup:

project setup

I'm trying to import Reservations.py in queries.py

I did:

from ..entities import Reservation
import sys
sys.path.append('..entities/')
from entities import Reservation

any ideas??

the first thing I tried looks good before I run it, but when I try to run it says:

ImportError: attempted relative import beyond top-level package

HMT
  • 21
  • 5
  • Please include your full project structure as text rendering, and also you have omitted where `queries.py` actually is. Moreover, it appears that wherever the `queries` module lies (I assume either `data_analysis` or `sql_dir`) is already a top level module so that's the source of the error. – metatoaster Apr 26 '23 at 11:31
  • That is correct, queries.py lies in sql_dir , so how can I make it work? I also put a better picture in the question – HMT Apr 26 '23 at 12:57
  • Does this answer your question? [beyond top level package error in relative import](https://stackoverflow.com/questions/30669474/beyond-top-level-package-error-in-relative-import) – Pravash Panigrahi Apr 26 '23 at 13:13
  • I don't really understand what they are talking about, I don't want to execute the imported file, just to use the class in there. I removed the dots, now it looks like it works, but it is underlined red in PyCharm, is there a way to avoid that? – HMT Apr 26 '23 at 13:18
  • try using - from Hotel Reservations.entities import Reservations – Pravash Panigrahi Apr 26 '23 at 13:21
  • Unresolved reference Hotel Reservations Unresolved reference entities – HMT Apr 26 '23 at 13:26
  • In that case you need to give your project directory path in `Mark Directory As` -> `Source Root` in PyCharm – Pravash Panigrahi Apr 26 '23 at 13:31

1 Answers1

0

Your scenario will work, If you are in the Hotel Reservation directory, sql_dir and entities are separate packages.

Conclusion: ..entities imports are only allowed within a package.

You can try -

from Hotel Reservations.entities import Reservation

You also need to give your project directory path in Mark Directory As -> Source Root in PyCharm

Pravash Panigrahi
  • 701
  • 2
  • 7
  • 21
  • This answer worked, but I have to add that I used the same imports I had initially, just without the dots. – HMT Apr 26 '23 at 16:02