Search code examples
pythonpython-import

ImportError: attempted relative import beyond top-level package, no red lines on code


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


Solution

  • 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