7

I get the error below when I add 'graphene_django' inside INSTALLED_APPS in the settings.py.

After running

python3 manage.py runserver

graphene_django is installed successfully using

pip install django graphene_django

This is full error that I get:

Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 115, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 381, in execute
    autoreload.check_errors(django.setup)()
  File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 223, in create
    import_module(entry)
  File "/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/usr/local/lib/python3.9/site-packages/graphene_django/__init__.py", line 1, in <module>
    from .fields import DjangoConnectionField, DjangoListField
  File "/usr/local/lib/python3.9/site-packages/graphene_django/fields.py", line 18, in <module>
    from .utils import maybe_queryset
  File "/usr/local/lib/python3.9/site-packages/graphene_django/utils/__init__.py", line 2, in <module>
    from .utils import (
  File "/usr/local/lib/python3.9/site-packages/graphene_django/utils/utils.py", line 6, in <module>
    from django.utils.encoding import force_text
ImportError: cannot import name 'force_text' from 'django.utils.encoding' (/usr/local/lib/python3.9/site-packages/django/utils/encoding.py)

Any idea on what's going wrong here?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Byusa
  • 2,279
  • 1
  • 16
  • 21
  • What version of django are you using? Upgrade it to the latest. – Jerven Clark Dec 30 '21 at 04:20
  • this solution it works for me- https://stackoverflow.com/a/70679791/16697782 – Ganesh Teli Jan 12 '22 at 10:22
  • Does this answer your question? [import error 'force\_text' from 'django.utils.encoding'](https://stackoverflow.com/questions/70382084/import-error-force-text-from-django-utils-encoding) – outis Jan 31 '22 at 15:22

4 Answers4

10

force_text is removed from Django 4.0

You can add this code to top of your settings.py :

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

Ganesh sali
  • 116
  • 1
  • 5
2

Install graphene-django in this way. This resolves the issue for me.

pip install "graphene-django==3.0.0b7"
Abir Hossain
  • 111
  • 7
1

You can install graphene-django==3.0.0b7

It's beta version, but i don't know why graphene-django 2.15 does'nt work, when patch note said they fixed issue since 2.8.1 version.

Thank's to @Behoston from this Github issue for this solution

yoles
  • 21
  • 1
0

Use from django.utils.encoding imort force_str instead of force_text

SLDem
  • 2,065
  • 1
  • 8
  • 28
  • 1
    I did not import ```force_text``` anywhere in my code – Byusa Dec 30 '21 at 00:00
  • thats what your error is, on the latest django version `force_str` is used but your python tries using `force_text` which means either you made a mistake or your django is outdated/messed up, you can try either updating or reinstalling it – SLDem Dec 30 '21 at 09:11
  • @SLDem: It's being included by `graphene-django`, which is why he can't change it without hacking the package, forking it and maintaining his own source code or engaging in some such endeavor. But the beta version of `graphene-django` works (3.0.0b7) as per Abir Hossain's answer. It just has to be installed speciflcally by version number (see his answer) until it's out of beta. – Teekin May 20 '22 at 13:45