我已经开始使用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
您可以在最小的repo中看到代码here的当前状态。
我认为我的问题与如何运行服务器有关:
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.
最佳答案
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.py
和ingredients
目录移动到内部cookbook
目录,并将INSTALLED_APPS
中的条目更改为'cookbook.ingredients'
,如this repo中所示。