Search code examples
pythondjangodjango-modelspython-import

ImportError: attempted relative import beyond top-level package django


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


Solution

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

    from books.models import Book