how to resolve django : ImportError: cannot import name 'parse_header' from 'django.http.multipartparser'
Asked Answered
R

2

11

My application was running fine till few days back, but now all of a sudden i'm seeing this error and not sure what it means, please help.

Error:

File "/myproj/myapp/urls.py", line 8, in <module>
from rest_framework import routers
File "/usr/local/lib/python3.8/site-packages/rest_framework/routers.py", line 22, in <module>
from rest_framework import views
File "/usr/local/lib/python3.8/site-packages/rest_framework/views.py", line 15, in <module>
from rest_framework.request import Request
File "/usr/local/lib/python3.8/site-packages/rest_framework/request.py", line 17, in <module>
from django.http.multipartparser import parse_header
ImportError: cannot import name 'parse_header' from 'django.http.multipartparser' (/usr/local/lib/python3.8/site-packages/django/http/multipartparser.py)

my line 8 of urls.py is:

from rest_framework import routers

and i could see the parse_header method in the multipartparser.py file PFA

multipartparser file

Reconstructionist answered 17/4, 2023 at 12:23 Comment(0)
B
24

In my opinion, this can be caused by two things:

  1. Problem with installing Django itself, and in this case I would recommend uninstalling and then reinstalling Django using pip install django
  2. Version mismatch between Django and djangorestframework, check if the versions of these two packages are compatible.

Please find the documentation link for the release notes attached below:

https://www.django-rest-framework.org/community/release-notes/

Beaudoin answered 17/4, 2023 at 14:59 Comment(4)
thanks Ahmad, it was due to django and restframework version mismatch :)Reconstructionist
Version mismatch between Django and djangorestframework, check if the versions of these two packages are compatible. this answer was correct for me. I deleted restframework version number in requirements and reinstalled it again. it worked for meWrestling
Good tip, this should be marked the answer. I got this error after upgrading to Django 4.2. pip install -U djangorestframework fixed it for me (3.13.1 --> 3.14.0 )Underhanded
After checking that it was a version mismatch, I used pip install --upgrade djangorestframework to upgrade rest framework to the latest version and my error is gone. ThanksDeppy
B
0

I solved this problem by removing the version for djangorestframework. It was only because of the version compatibility between django and its rest_framework.

Bocock answered 17/7 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.