如何通过教程解决使用graphene Django基本设置的重要错误?

2024-10-01 13:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经开始使用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。或者我是怎么进口的。但我不知道如何在这里进行更多的调试。在

每件事都和教程里说的一模一样,对我来说没有什么特别不正确的。如果这很重要的话,我也将Linux与virtualenv一起使用。在

如果你需要更多的信息请告诉我,我会仔细监视线程。在

编辑:任命!我用python3和python2.7运行过它,但都不起作用。以下是线索:

^{pr2}$

Tags: djangoimportenvhomeschemalibpackagesexception
2条回答
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中的条目改为{},如this repo。在

执行此行

pip install graphene_django

相关问题 更多 >