Search code examples
pythondjangopython-importgraphene-python

How to resolve an ImportError working with a graphene django basic setup through tutorial?


I have started working through the graphene_django tutorial for a basic setup using Django and GraphQL.

However, I am unable to progress any further due to an import error:

ImportError at /graphql
Could not import 'cookbook.schema.schema' for Graphene setting 'SCHEMA'. ImportError: No module named schema.
Request Method: GET
Request URL:    http://127.0.0.1:8000/graphql
Django Version: 1.11.2
Exception Type: ImportError
Exception Value:    
Could not import 'cookbook.schema.schema' for Graphene setting 'SCHEMA'. ImportError: No module named schema.
Exception Location: /home/mackie/Code/graphene_django_tutorial/env/local/lib/python2.7/site-packages/graphene_django/settings.py in import_from_string, line 78
Python Executable:  /home/mackie/Code/graphene_django_tutorial/env/bin/python
Python Version: 2.7.12
Python Path:    
['/home/mackie/Code/graphene_django_tutorial/cookbook',
 '/home/mackie/Code/graphene_django_tutorial/env/lib/python2.7',
 '/home/mackie/Code/graphene_django_tutorial/env/lib/python2.7/plat-x86_64-linux-gnu',
 '/home/mackie/Code/graphene_django_tutorial/env/lib/python2.7/lib-tk',
 '/home/mackie/Code/graphene_django_tutorial/env/lib/python2.7/lib-old',
 '/home/mackie/Code/graphene_django_tutorial/env/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/mackie/Code/graphene_django_tutorial/env/local/lib/python2.7/site-packages',
 '/home/mackie/Code/graphene_django_tutorial/env/lib/python2.7/site-packages']
Server time:    Sun, 4 Jun 2017 16:29:46 +0000

You can see the current state of the code here in a minimal repo.

I think that my problem has something to do with how I'm running the server: python3 manage.py runserver. Or maybe how I am importing. But I don't know how to do any more debugging from here.

Everything is exactly as stated in the tutorial and nothing seems especially incorrect to me. I'm using Linux with virtualenv as well if that matters.

Let me know if you need any more information I'll be carefully monitoring the thread.

EDIT: Appologies! I had run it with python3 and python 2.7, neither had worked. Here is the trace:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/graphql

Django Version: 1.10.6
Python Version: 2.7.12
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'graphene_django',
 'ingredients']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
  42.             response = get_response(request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py" in view
  62.             self = cls(**initkwargs)

File "/usr/local/lib/python2.7/dist-packages/graphene_django/views.py" in __init__
  70.             schema = graphene_settings.SCHEMA

File "/usr/local/lib/python2.7/dist-packages/graphene_django/settings.py" in __getattr__
  116.             val = perform_import(val, attr)

File "/usr/local/lib/python2.7/dist-packages/graphene_django/settings.py" in perform_import
  60.         return import_from_string(val, setting_name)

File "/usr/local/lib/python2.7/dist-packages/graphene_django/settings.py" in import_from_string
  78.         raise ImportError(msg)

Exception Type: ImportError at /graphql
Exception Value: Could not import 'cookbook.schema.schema' for Graphene setting 'SCHEMA'. ImportError: No module named schema.

Solution

  • GRAPHENE = {
        'SCHEMA': 'cookbook.schema.schema'
    }
    

    For cookbook.schema to be importable, you would need to place schema.py in the inner cookbook directory (the one that contains settings.py). At the moment, you have it in the outer cookbook directory (the one that contains manage.py). To import it from the outer directory, you would want 'schema.schema' in the setting.

    There is similar confusion when the tutorial says to add 'ingredients' to the INSTALLED_APPS setting (suggests it should be in the outer cookbook directory), but the code includes imports like from cookbook.ingredients.models import ... (suggests that ingredients should be in the inner cookbook directory).

    You could try moving both the schema.py and the ingredients directory into the inner cookbook directory, and change the entry in INSTALLED_APPS to 'cookbook.ingredients', as in this repo.