3

我已经开始使用 Django 和 GraphQL 进行基本设置的graphene_django 教程

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

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

您可以在最小的 repo 中 查看代码的当前状态。

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

一切都与教程中所述完全相同,对我来说似乎没有什么特别不正确的。如果这很重要,我也在使用带有 virtualenv 的 Linux。

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

编辑:道歉!我用 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.
4

2 回答 2

4
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目录中)。

您可以尝试将 theschema.pyingredients目录都移动到内部cookbook目录中,并将条目更改INSTALLED_APPS'cookbook.ingredients',如在此 repo中。

于 2017-06-04T17:13:32.387 回答
0

执行这一行

pip install graphene_django
于 2019-10-16T11:14:16.060 回答