Search code examples
python-3.xdjango-2.0

How to fix this error when starting Django service?--Unhandled exception in thread started by


I'm using python3.6 and Django2.0, but the code is supposed to be written in Django1.x. An error occurred when I tried to start the service. This is my first time to contact Django and I hope to get help.

It should be an error in the urls.py file

This is urls.py. This file in the book of .it's GitHub is 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')
]

This error Message,when I run "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

Solution

  • The error occurred because urllib2 was referenced in the sklearn library and datasets library. I'm just going to change the code in the file Original 24-31 contents: Try:

    # 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
    
    Reason: the contents of urllib2 are removed because of an error in python3.x Now:
    # Python 3 +
    The from urllib. Request the import urlopen
    The from urllib. Error import HTTPError