3
votes

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
you can import like: from project_root.app_name.models import * - Meha Parekh
@MehaParekh I tried this and got ModuleNotFoundError: No module named 'yantra_packs.packsapp' - Rahul Sharma
did you add packsapp in installed_apps in settings.py file? - Meha Parekh
yes I have both of them in my Installed_apps - Rahul Sharma
Did you import something from grn into packsapp? - Meha Parekh

1 Answers

5
votes

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 *