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

Time:08-31

I'm writing a site on Django and faced the problem that when relative importing from a directory located in the same directory as the directory in which the import is carried out, the following error appears:

ImportError: attempted relative import beyond top-level package

here is my import,which is in the file online_book/shop-cart/cart.py

from ..books.models import Book

the Book model is in the file online-book/books/models.py

it`s my project structure

help me find solution to this problem please

CodePudding user response:

imports are relative to where the manage.py is
so your import should be like this:

from books.models import Book

CodePudding user response:

Set dir online_book as root directory in settings

  • Related