Django Model Import Error: ValueError: attempted relative import beyond top-level package

Viewed 3179

I have created a new app "grn" in my django project and tried to import the models from another app named "packsapp" in the same project like this:

Models.py

from ..packsapp.models import *

But I got the following error:

ValueError: attempted relative import beyond top-level package

Here's the structure of the app:

yantra_packs

grn
--migrations
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py
media
packsapp
--migrations
  templates
  templatetags
  views1
    __init__.py
    apps.py
    decorators.py
    forms.py
    models.py
    urls.py
    views.py

How can I import the models of the packsapp in the grn ??

1 Answers

The root directory of a Django project is not a Python package or module. So relative imports across Django apps will not work. Use an absolute import instead:

from packsapp.models import *
Related