Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ??

like image 945
Rahul Sharma Avatar asked Sep 08 '25 00:09

Rahul Sharma


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 *
like image 91
Crazy PhD Avatar answered Sep 09 '25 16:09

Crazy PhD