Python 无法导入名称包括
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37649934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot import name include
提问by anand mbs
I am following the tutorial in django and obtaining the following error.
我正在关注 django 中的教程并获得以下错误。
Found few solutions but it didn't resolve my error. The tutorial link is this.
找到了几个解决方案,但没有解决我的错误。教程链接是这个。
The error which I obtained is attached below. Please help me with this before duplicating the question. I am learning python and django services so it would help ma a lot.
我得到的错误附在下面。在复制问题之前,请帮助我解决这个问题。我正在学习 python 和 django 服务,所以这对我有很大帮助。
ImportError at /polls/
cannot import name include
Request Method: GET
Request URL: http://localhost:8000/polls/
Django Version: 1.3.1
Exception Type: ImportError
Exception Value:
cannot import name include
Exception Location: /home/next/Desktop/MBS/python_files/progs/django_examples/mysite/../mysite/urls.py in <module>, line 3
Python Executable: /usr/bin/python
Python Version: 2.7.3
Python Path:
['/home/next/Desktop/MBS/python_files/progs/django_examples/mysite',
'/usr/local/lib/python2.7/dist-packages/PyQRCode-1.2-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/setuptools-22.0.0-py2.7.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PIL',
'/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-control-panel',
'/usr/lib/python2.7/dist-packages/ubuntuone-couch',
'/usr/lib/python2.7/dist-packages/ubuntuone-installer',
'/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']
Server time: Mon, 6 Jun 2016 00:03:30 -0500
Edit 1: I will attach the urls.py of both so it would help to identify
编辑 1:我将附上两者的 urls.py 以帮助识别
in polls/urls.py
在民意调查/ urls.py
from django.conf.urls import url
from django.conf.urls import include
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
and mysite/urls.py
和 mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
Edit 2:The trace log is attached below
编辑 2:跟踪日志附在下面
Environment:
Request Method: GET
Request URL: http://localhost:8000/polls/
Django Version: 1.3.1
Python Version: 2.7.3
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
99. request.path_info)
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
269. for pattern in self.url_patterns:
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_url_patterns
298. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib/python2.7/dist-packages/django/core/urlresolvers.py" in _get_urlconf_module
293. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/home/next/Desktop/MBS/python_files/progs/django_examples/mysite/../mysite/urls.py" in <module>
1. from django.conf.urls import include, url
Exception Type: ImportError at /polls/
Exception Value: cannot import name include
回答by anand mbs
I have found the solution for the above mentioned question. The version of the django is the main cause. Updating the django version cleared the error which was raised previously.
我已经找到了上述问题的解决方案。django 的版本是主要原因。更新 django 版本清除了之前引发的错误。
the version before updating was 1.3 and now the version is 1.10
更新前版本是1.3,现在版本是1.10
To update the django version,
要更新 Django 版本,
$ sudo pip install -U Django
回答by Raja Simon
well you have to include include
in polls
urls
那么你必须包括include
在polls
urls
from django.conf.urls import include