我正在使用python3.6和Django2.0,但是代码应该写在Django1.x中。尝试启动服务时发生错误。这是我第一次联系Django,希望获得帮助。
urls.py文件中应该有错误
这是urls.py。 .it的GitHub书中的文件为https://github.com/ai2010/machine_learning_for_the_web/tree/master/chapter_7/server_movierecsys
#from django.conf.urls import patterns, include, url
from django.conf.urls import include, url
from django.contrib import admin
from books_recsys_app.api import UsersList
import books_recsys_app.views
import rest_framework_swagger
from books_recsys_app.views import home, auth, signout, rate_movie, movies_recs
from rest_framework_swagger.views import get_swagger_view
schema_view = get_swagger_view(title='API name')
urlpatterns = [
url(r'^docs/', schema_view)
]
urlpatterns += [
url(r'^$', home, name='home'),
url(r'^auth/', auth, name='auth'),
url(r'^signout/',signout,name='signout'),
url(r'^rate_movie/',rate_movie,name='rate_movie'),
url(r'^movies-recs/',movies_recs,name='movies_recs'),
url(r'^admin/', include(admin.site.urls)),
url(r'^users-list/',UsersList.as_view(),name='users-list')
]
当我运行“ python3 manage.py runserver localhost:8000”时出现此错误消息
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10b52abf8>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 366, in _run_checks
return checks.run_checks(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "/Users/changxuan/PycharmProjects/reconmendationSystem/server_movierecsys/server_movierecsys/urls.py", line 5, in <module>
import books_recsys_app.views
File "/Users/changxuan/PycharmProjects/reconmendationSystem/server_movierecsys/books_recsys_app/views.py", line 17, in <module>
from sklearn.feature_extraction.text import TfidfVectorizer
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/feature_extraction/__init__.py", line 10, in <module>
from . import text
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/feature_extraction/text.py", line 30, in <module>
from ..preprocessing import normalize
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/preprocessing/__init__.py", line 6, in <module>
from ._function_transformer import FunctionTransformer
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/preprocessing/_function_transformer.py", line 5, in <module>
from ..utils.testing import assert_allclose_dense_sparse
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/utils/testing.py", line 26, in <module>
from urllib2 import urlopen
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/urllib2.py", line 220
raise AttributeError, attr
^
SyntaxError: invalid syntax
答案 0 :(得分:0)
发生错误是因为在sklearn库和数据集库中引用了urllib2。我只是要更改文件中的代码 24-31的原始内容: 试试:
# Python 2
The from urllib2 import urlopen
The from urllib2 import HTTPError
Except ImportError:
# Python 3 +
The from urllib. Request the import urlopen
The from urllib. Error import HTTPError
原因:由于python3.x中的错误而删除了urllib2的内容
现在:
# Python 3 +
The from urllib. Request the import urlopen
The from urllib. Error import HTTPError