ImportError: cannot import name 'patterns' from 'django.conf.urls'
Fixes `ImportError: cannot import name 'patterns' from 'django.conf.urls'` in django. Use when the traceback points at django.conf.urls and names patterns, usually right after a Django upgrade. Not for 404s, reversed-URL NoReverseMatch errors, or 'No module named django' (that is a missing install).
ImportError: cannot import name 'patterns' from 'django.conf.urls'
TL;DR
Your code (or a dependency) still imports patterns from django.conf.urls, which Django removed in 1.10. Replace patterns('', ...) with a plain Python list of url() entries. If the import lives in a third-party package, upgrade that package to a version that supports your Django.
The error
ImportError: cannot import name 'patterns' from 'django.conf.urls'Fix it
- Find every import of patterns: run grep -rn "from django.conf.urls import" --include=*.py . and note each file that pulls in patterns.
Expected: You get a short list of files, usually urls.py files and maybe an old third-party package.
- In your own code, replace the old call: patterns('', url(r'^$', view)) becomes a plain list [url(r'^$', view)]. The first string argument (the prefix) is simply dropped.
Expected: urls.py imports cleanly and python manage.py check runs without the ImportError.
- If the import is inside site-packages (a dependency), upgrade it: pip install -U [package]. Waliki, for example, fixed this in a later release.
Expected: The dependency imports fine against your Django version.
- As a last resort for an unmaintained package, pin Django below 1.10, but plan the migration since that Django is long out of support.
Expected: App boots, with a note to revisit the pin.
When to use this
Use when the traceback points at django.conf.urls and names patterns, usually right after a Django upgrade.
When NOT to use this
Not for 404s, reversed-URL NoReverseMatch errors, or 'No module named django' (that is a missing install).
Version notes
patterns() existed up to Django 1.8, deprecated in 1.8, removed in 1.10. Any code importing it needs Django < 1.10 or a rewrite.
Why it happens
Django 1.10 deleted the patterns() helper along with the old string-view-prefix style. Old tutorials and old packages still import it, so upgrading Django past 1.9 breaks them at import time.
Edge cases
Some packages import patterns conditionally; a grep that finds nothing in your code means the culprit is a dependency, check pip freeze and the full traceback.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.