import error ‘force_text’ from ‘django.utils.encoding’

We Are Going To Discuss About import error ‘force_text’ from ‘django.utils.encoding’. So lets Start this Python Article.

import error ‘force_text’ from ‘django.utils.encoding’

  1. How to solve import error 'force_text' from 'django.utils.encoding'

    in django 4.0 we dont have force_text
    https://docs.djangoproject.com/en/4.0/ref/utils/#module-django.utils.encoding
    instead change force_text to force_str
    linux:
    YOUR_VENV/lib/PYTHON_VERSION/site-packages/graphene_django/utils/utils.py
    windows:
    YOUR_VENV/lib/site-packages/graphene_django/utils/utils.py
    from django.utils.encoding import force_text
    to
    from django.utils.encoding import force_str
    and
    def _camelize_django_str(s): if isinstance(s, Promise): s = force_text(s) return to_camel_case(s) if isinstance(s, six.string_types) else s
    to
    def _camelize_django_str(s): if isinstance(s, Promise): s = force_str(s) return to_camel_case(s) if isinstance(s, six.string_types) else s

  2. import error 'force_text' from 'django.utils.encoding'

    in django 4.0 we dont have force_text
    https://docs.djangoproject.com/en/4.0/ref/utils/#module-django.utils.encoding
    instead change force_text to force_str
    linux:
    YOUR_VENV/lib/PYTHON_VERSION/site-packages/graphene_django/utils/utils.py
    windows:
    YOUR_VENV/lib/site-packages/graphene_django/utils/utils.py
    from django.utils.encoding import force_text
    to
    from django.utils.encoding import force_str
    and
    def _camelize_django_str(s): if isinstance(s, Promise): s = force_text(s) return to_camel_case(s) if isinstance(s, six.string_types) else s
    to
    def _camelize_django_str(s): if isinstance(s, Promise): s = force_str(s) return to_camel_case(s) if isinstance(s, six.string_types) else s

Solution 1

in django 4.0 we dont have force_text

https://docs.djangoproject.com/en/4.0/ref/utils/#module-django.utils.encoding

instead change force_text to force_str

linux:

YOUR_VENV/lib/PYTHON_VERSION/site-packages/graphene_django/utils/utils.py

windows:

YOUR_VENV/lib/site-packages/graphene_django/utils/utils.py

from django.utils.encoding import force_text

to

from django.utils.encoding import force_str

and

def _camelize_django_str(s):
    if isinstance(s, Promise):
        s = force_text(s)
    return to_camel_case(s) if isinstance(s, six.string_types) else s

to

def _camelize_django_str(s):
    if isinstance(s, Promise):
        s = force_str(s)
    return to_camel_case(s) if isinstance(s, six.string_types) else s

Original Author Osman Of This Content

Solution 2

Based on answer given by @Osman.

The problem seems to be occuring with Django-4. Till the PR gets merged, probably this monkeypatching might work (not tested in prod):

import django
from django.utils.encoding import force_str
django.utils.encoding.force_text = force_str

Put this in entryfile. I kept it in settings.py for time being.

Original Author Blaze Of This Content

Solution 3

“force_text” is removed from Django 4.0:

Features removed in 4.0

But graphene_django still uses “force_text” in utils.py.

utils.py:

from django.utils.encoding import force_text # Line 6

s = force_text(s) # Line 29

So you need to replace “force_text” with “force_str” as shown below:

utils.py:

from django.utils.encoding import force_str# Line 6

s = force_str(s) # Line 29

These are the paths to utils.py for Linux and Windows:

Linux:

<your_venv>/lib/<python_version>/site-packages/graphene_django/utils/utils.py

Windows:

<your_venv>/lib/site-packages/graphene_django/utils/utils.py

Original Author Kai – Kazuya Ito Of This Content

Solution 4

In Django version 4> just paste this snippet to your settinsg.py. Preferably on the top

    import django
    from django.utils.encoding import force_str
    django.utils.encoding.force_text = force_str

Original Author zeph Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment