I am running the project and shows this error. I have googled and tried some of the solutions but doesn't seem to work.
File "/home/bs-094/Dev/backend/busgroup-backend/src/bus_portal_backend/apps/inquiry/forms/inquiry_details.py", line 2, in <module>
from ..models.inquiry import Inquiry
ValueError: attempted relative import beyond top-level package
My folder structure is
busgroup-backend
src
bus_portal_backend
apps
inquiry
models
_init_.py
inquiry.py
forms
inquiry_details.py
It would be great help is anyone helps me solve the problem. I am fairly new to django. Thanks
EDIT : Here's what solved my problem , I had to import in this way
from bus_portal_backend.apps.inquiry.models import Inquiry
Also, I was running with debugger. My script path was busgroup-backend/src/manage.py
, I changed it to /home/bs-094/Dev/backend/busgroup-backend/src/manage.py
, which made me run the project successfully.
forms
does not appear to contain an __init__.py
file, and so Python doesn't think it's a package. Therefore an attempt to import from ..models
is an attempt to ascend above the current package's root (because ...busgroup-backend/src/bus_portal_backend/apps/inquiry/forms/inquiry_details.py
is actually a standalone module).
Relative imports only work within a package. Therefore you should make both forms
and models
subpackages of inquiry
by creating inquiry__init__.py
as well.
If you import from ..models.inquiry import Inquiry
then ..
refers to
busgroup-backend
src
bus_portal_backend
apps
Which does not have an __init__.py
and thusly is not considered a package by python.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.