Home > Mobile >  ImportError: attempted relative import beyond top-level package?
ImportError: attempted relative import beyond top-level package?

Time:10-30

My code structure:

code structure 1

code structure 2

When I import model from accounts app to to_do app:

#in to_do/models.py
from ..accounts.models import Account

I have this error:

 File "/home/ghost/projects/django_projects/To_Do_App/to_do_list/to_do/models.py", line 2, in <module>
    from ..accounts.models import Account
ImportError: attempted relative import beyond top-level package

How to fix it?

CodePudding user response:

Be sure to have an __init__.py file in the parent directory. This is how python recognizes it as a package.

CodePudding user response:

I tried to import my model this way from accounts.models import Account
and now it works, but pycharm says that's not corectly

  • Related