Django 1.11安装django-facebook6.0.3遇ImportError问题求助
ImportError: No module named 'django.conf.urls.defaults' with django-facebook 6.0.3 and Django 1.11 Hey there, let's break down why this error is happening and how to fix it properly.
Why the Error Occurs
Django removed the django.conf.urls.defaults module starting from version 1.8, moving url and patterns directly into django.conf.urls. The django-facebook 6.0.3 package is outdated and still references the old module path, which is why you're hitting this import error.
Your existing try-except code didn't work because you added it to your own project files, but the error is coming from the third-party django-facebook package's urls.py file—so your code doesn't affect that file at all.
Solution 1: Manually Patch the django-facebook urls.py File
This is the most straightforward fix for this specific error:
- Navigate to the file mentioned in the error:
path/lib/python3.5/site-packages/django_facebook/urls.py - Replace the line:
With:from django.conf.urls.defaults import patterns, urlfrom django.conf.urls import url # We'll handle the `patterns` function next - Look for any uses of
patterns('', ...)in the file and convert them to a list ofurl()objects. For example:- Old code (using deprecated
patterns):urlpatterns = patterns('', url(r'^login/$', ...), # more URL definitions ) - Updated code (compatible with Django 1.11+):
urlpatterns = [ url(r'^login/$', ...), # more URL definitions ]
patternsbut throws deprecation warnings, so converting to a list is cleaner and avoids future issues. - Old code (using deprecated
Solution 2: Monkey Patching (Temporary Workaround)
If you don't want to modify third-party package files directly, you can add a monkey patch to your project's settings.py to trick django-facebook into finding the module:
# Add this at the TOP of your settings.py import django.conf.urls import sys # Create a dummy module that mirrors the old defaults structure sys.modules['django.conf.urls.defaults'] = django.conf.urls
This will make the import from django.conf.urls.defaults import ... point to the current django.conf.urls module. Note that this is a temporary band-aid—you might still run into other compatibility problems with the outdated django-facebook package.
Long-Term Recommendation
django-facebook 6.0.3 is quite old and likely unmaintained. You'll probably run into more compatibility issues with Django 1.11 down the line. Consider switching to an actively maintained package like django-allauth for Facebook authentication—it supports modern Django versions and handles OAuth flows more reliably.
内容的提问来源于stack exchange,提问作者Mark

