ImportError: cannot import name 'url' from 'django.conf.urls'
Fixes ImportError: cannot import name 'url' from 'django.conf.urls' Use when upgrading to Django 4+ and `from django.conf.urls import url` blows up Not for: you are on Django < 4 (it still works there, just deprecated) or the error is about a different name
How to fix ImportError: cannot import name 'url' from 'django.conf.urls'
Django 4 removed django.conf.urls.url(). Replace it: for regex patterns use from django.urls import re_path and swap url( for re_path(; for simple paths use path(). A find-and-replace across your urls.py files does most projects in one pass.
The exact error
ImportError: cannot import name 'url' from 'django.conf.urls'Fix it
- In urls.py change the import:
from django.conf.urls import url->from django.urls import re_path(orpath).
Expected: no more django.conf.urls import
- Replace
url(calls withre_path((regex) or rewrite aspath((simple routes).
Expected: python manage.py check passes
- Large project?
pip install django-upgradeand run it: it rewrites these automatically.
Expected: diff shows only url->re_path/path changes
When this applies
upgrading to Django 4+ and from django.conf.urls import url blows up
When this does NOT apply
you are on Django < 4 (it still works there, just deprecated) or the error is about a different name
Versions
Django >= 4.0
Why it happens
url() was deprecated since Django 3.1 and removed in 4.0; django.urls.path/re_path are the replacements.
Edge cases
Third-party packages can trigger this too; upgrade them rather than downgrading Django.
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.