2017-06-04 89 views
2

我已經開始通過使用Django和GraphQL進行基本設置的graphene_django tutorial如何通過教程解決使用石墨烯django基本設置的ImportError?

但是,我無法進展的更多由於導入錯誤:

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。或者,也許我是如何進口的。但我不知道如何從這裏做更多的調試。

一切都完全如教程中所述,沒有什麼似乎特別不正確的我。如果有問題,我也會用virtualenv來使用Linux。

讓我知道你是否需要更多信息我會仔細監控線程。

編輯:Appologies!我用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. 
+0

你說你運行'python3 manage.py runserver'沒有任何意義,但錯誤顯示Python 2.7。你還沒有顯示完整的回溯,這將顯示導入失敗的地方。 – Alasdair

+0

對不起!我已經在兩種情況下運行它,我將完整的軌跡添加到原始帖子中。 –

回答

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入門到'cookbook.ingredients',如this repo

+1

你是絕對正確的,本教程有一些錯誤的路徑,導致我陷入了一個瘋狂的誘惑調試路徑。非常感謝! 任何人在將來遇到與教程有關的問題時,都會看到這部分的回購:https://github.com/graphql-python/graphene-django/tree/master/examples/cookbook –