Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm implementing a graphql solution using python, graphene and django and I'm getting the following import error:

Result: Failure Exception: ImportError: cannot import name 'force_text' from 'django.utils.encoding'

"/home/site/wwwroot/.python_packages/lib/site-packages/graphene_django/utils/utils.py", line 6, in <module> from django.utils.encoding import force_text

I'm not sure about the versions and whether I need to import an additional module. My requirements.txt is like:

graphene>=2.1,<3
graphene-django>=2.1,<3
graphql-core>=2.1,<3
graphql-relay==2.0.1
django-filter>=2

Has someone had a similar problem and can look at the versions that I use? Thanks

like image 222
Gerd Avatar asked Dec 16 '21 16:12

Gerd


5 Answers

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
like image 133
Osman Avatar answered Oct 19 '22 19:10

Osman


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.

like image 24
Blaze Avatar answered Oct 19 '22 18:10

Blaze


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
like image 11
zeph Avatar answered Oct 19 '22 18:10

zeph


"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

like image 10
Kai - Kazuya Ito Avatar answered Oct 19 '22 19:10

Kai - Kazuya Ito


adding the following to the requirements.txt solved it:

django<=3
like image 3
Gerd Avatar answered Oct 19 '22 18:10

Gerd