graphql-python / graphene-django

Build powerful, efficient, and flexible GraphQL APIs with seamless Django integration.

Home Page:http://docs.graphene-python.org/projects/django/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Django v.4.0 and graphene-django v.2.15.0

hannody opened this issue · comments

Note: for support questions, please use stackoverflow. This repository's issues are reserved for feature requests and bug reports.

  • What is the current behavior?

Running Django 4.0 server fails after adding/installing graphene-django v.2.15.0 to the project.
"ImportError: cannot import name 'force_text' from 'django.utils.encoding'"

  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via
    a github repo, https://repl.it or similar (you can use this template as a starting point: https://repl.it/@jkimbo/Graphene-Django-Example).

  • **Steps to reproduce:
    1- mkvirtualenv dj4
    2- pip install django
    3- django-admin startproject core .
    4- python manage.py runserver (Django server is up and running)
    5- pip install graphene-django
    6- python manage.py runserver ( ImportError: cannot import name 'force_text' from 'django.utils.encoding' (/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/django/utils/encoding.py)

  • What is the expected behavior?

Django server should be up and running. downgrading django to 3.2.9 will make the project works!

  • What is the motivation / use case for changing the behavior?

  • Please tell us about your environment:

    • Version: Python 3.8, Django 4.0, graphene-django v.2.15.0
    • Platform: Ubuntu 20.04
  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow)

  • **Stacktrace
    $ python manage.py runserver
    Watching for file changes with StatReloader
    Exception in thread django-main-thread:
    Traceback (most recent call last):
    File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
    File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 115, in inner_run
    autoreload.raise_last_exception()
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/django/core/management/init.py", line 381, in execute
    autoreload.check_errors(django.setup)()
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/django/init.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/django/apps/config.py", line 223, in create
    import_module(entry)
    File "/usr/lib/python3.8/importlib/init.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
    File "", line 1014, in _gcd_import
    File "", line 991, in _find_and_load
    File "", line 975, in _find_and_load_unlocked
    File "", line 671, in _load_unlocked
    File "", line 848, in exec_module
    File "", line 219, in _call_with_frames_removed
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/graphene_django/init.py", line 1, in
    from .fields import DjangoConnectionField, DjangoListField
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/graphene_django/fields.py", line 18, in
    from .utils import maybe_queryset
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/graphene_django/utils/init.py", line 2, in
    from .utils import (
    File "/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/graphene_django/utils/utils.py", line 6, in
    from django.utils.encoding import force_text
    ImportError: cannot import name 'force_text' from 'django.utils.encoding' (/home/fake_user/.virtualenvs/dj4/lib/python3.8/site-packages/django/utils/encoding.py)

Got the same issue.

def force_text(s, encoding='utf-8', strings_only=False, errors='strict'): warnings.warn( 'force_text() is deprecated in favor of force_str().', RemovedInDjango40Warning, stacklevel=2, ) return force_str(s, encoding, strings_only, errors)
found this on django=3.2 django/utils/encoding.py line 110

I followed an answer on this stackoverflow post to resolve the issue:
https://stackoverflow.com/questions/70382084/import-error-force-text-from-django-utils-encoding/70389991#70389991
Change an import statement in site-packages/graphene_django/utils/utils.py so that it imports force_str and not force_text.

Just started learning graphql using django-graphene. This issue was such a road blocker for me. :(

version graphene-django==3.0.0b7 works with Django 4.0 but I really want to see new release (this beta version is 1yo)

Can we get a new prod release to get Django 4.0 working?

Bump on this! Having the same issue with Django 4.0. Really slowing down releases to staging/production since I have to hotfix the library code as per the stackoverflow post mentioned above.

Bump on this as well.

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.

Bump on this.

The fix by @jyotendra worked for me. Another option is to put it in the __init__.py that is next to settings.py

#1275 is merged. @hannody Can you please try it again?

I can confirm that this is solved for me using commit a7a8b3d under Django 4.0.2 (pip install git+git://github.com/graphql-python/graphene-django.git@a7a8b3dca6cee0ac7e94833535fea65911b507ac).

Will 2.16 be released anytime soon? Django 4.0 support would be really nice.

@ryanmerolle Working on it right now, i hope we will have something in a week 🌷

Much appreciated! I did not mean to come off as demanding or rude. I appreciate all the work you put into this project!

Happy to help get this out the door if I can. IIRC there was some mention of wanting to clean up the test suite prior to releasing?

Having the same issue . Waiting for the next version.
Using the monkey patch method suggested by Jyotendra for the time being.

How goes everything @ulgens? Is there anyway any of us can help?

Any update on this?

@ulgens how is progress coming along on the latest release? Just curious if there's anything I could do to help!

FYI if you have not seen the post, ulgens and team are stepping away from the project. This could get picked up by the larger graphql-python group, but still TBD. #1324

FYI if you have not seen the post, ulgens and team are stepping away from the project. This could get picked up by the larger graphql-python group, but still TBD. #1324

Look, we will now gently transition from graphene_django to strawberry, we can share our experience if you are interested.
Because strawberries are healthier, more prosperous, and architecturally more advanced.

@Luferov are you using Relay connections with strawberry?
From what I've seen on the repo the Relay spec is still to be implemented

i have same problem.
Django==4.1
Python 3.10.5

There is no solution?

@webafra You can work around it with a monkey patch in settings.py if you really need to, but recognize that it's a band-aid at best. (The below works for Django 4.0; haven't tested on Django 4.1.)

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

I don't want to put too much pressure on the devs but is this project still maintained? Django 4.0 was released 10 months ago, yet the current default version on PyPi is still incompatible with Django 4.0, the advised quickstart installation also heads to a non-existing package.

venv ❯ pip3 install --upgrade "graphene-django>=3"          
ERROR: Could not find a version that satisfies the requirement graphene-django>=3 (from versions: 1.0.dev20160909000001, 1.0.dev20160910000001, 1.0.dev20160917000001, 1.0.dev20160919000001, 1.0.dev20160919000002, 1.0.dev20160919000003, 1.0.dev20160919000004, 1.0.dev20160920000001, 1.0.dev20160922000001, 1.0, 1.1.0, 1.2.0, 1.2.1, 1.3, 2.0.dev2017072501, 2.0.dev2017072601, 2.0.dev2017073101, 2.0.dev2017083101, 2.0.0, 2.1rc0, 2.1rc1, 2.1.0, 2.2.0, 2.3.0, 2.3.2, 2.4.0, 2.5.0, 2.6.0, 2.7.0, 2.7.1, 2.8.0, 2.8.1, 2.8.2, 2.9.0, 2.9.1, 2.10.0, 2.10.1, 2.11.0, 2.11.1, 2.12.0, 2.12.1, 2.13.0, 2.14.0, 2.15.0, 3.0.0b1, 3.0.0b2, 3.0.0b3, 3.0.0b4, 3.0.0b5, 3.0.0b6, 3.0.0b7, 3.0.0b8)
ERROR: No matching distribution found for graphene-django>=3

I currently evaluating a switch from DRF to GraphQL for a new project and this circumstance does not put much trust into this otherwise really fine looking project, but it also seems like abandon ware for this reason.

@capital-G #705 (comment) There is a discussion about what is going on with the project, you may want to follow it 🌷

This issue should be resolved by #1275 and the latest release.