ImportError: cannot import name 'force_text' from 'django.utils.encoding'
Fixes ImportError: cannot import name 'force_text' from 'django.utils.encoding' Use when Django 4+ raises this from your code or a dependency Not for: Django < 4 (force_text still exists there, deprecated)
How to fix ImportError: cannot import name 'force_text' from 'django.utils.encoding'
force_text was renamed force_str in Django 3 and the old name removed in Django 4. In your code, replace from django.utils.encoding import force_text with force_str. If the traceback is inside a third-party package (graphene-django, django-elasticsearch-dsl, simplejwt...), upgrade that package instead of patching site-packages.
The exact error
ImportError: cannot import name 'force_text' from 'django.utils.encoding'Fix it
- Find the culprit:
grep -rn "force_text" --include=*.py .and check the traceback's file path.
Expected: you know whether it is your code or a library
- Your code: replace forcetext with forcestr (same behavior).
Expected: import works on Django 4+
- A library:
pip install -U [library].
Expected: fixed upstream release resolves it
When this applies
Django 4+ raises this from your code or a dependency
When this does NOT apply
Django < 4 (force_text still exists there, deprecated)
Versions
Django >= 4.0
Why it happens
Django dropped the Python-2-era text/bytes naming; forcetext -> forcestr.
Edge cases
smarttext -> smartstr and ugettext -> gettext were removed in the same cleanup; fix them together.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.