如何通过教程解决使用 graphene django 基本设置的 ImportError?

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

我已经开始使用 Django 和 GraphQL 完成 graphene_django tutorial 的基本设置。

但是,由于导入错误,我无法继续前进:

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

您可以在最小存储库中查看代码 here 的当前状态。

我认为我的问题与我运行设置服务器的方式有关:python3 manage.py runserver。或者也许我是如何进口的。但是我不知道如何从这里进行更多调试。

一切都与教程中所述完全一致,对我来说没有什么特别不对的地方。如果重要的话,我也在使用 Linux 和 virtualenv。

如果您需要任何更多信息,请告诉我,我会仔细监控线程。

编辑:抱歉!我有 运行 它与 python3 和 python 2.7,都没有用。这是踪迹:

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.
GRAPHENE = {
    'SCHEMA': 'cookbook.schema.schema'
}

要使 cookbook.schema 可导入,您需要将 schema.py 放入内部 cookbook 目录(包含 settings.py 的目录)。目前,它位于外部 cookbook 目录(包含 manage.py 的目录)。要从外部目录导入它,您需要在设置中设置 'schema.schema'

当教程说要将 'ingredients' 添加到 INSTALLED_APPS 设置(建议它应该在外部 cookbook 目录中)时,也存在类似的混淆,但代码包括导入,如from cookbook.ingredients.models import ...(建议 ingredients 应该在内部 cookbook 目录中)。

您可以尝试将 schema.pyingredients 目录移动到内部 cookbook 目录,并将 INSTALLED_APPS 中的条目更改为 'cookbook.ingredients' , 如 this repo.

执行这一行

pip install graphene_django