Likely cause
A Python NameError: name 'path' is not defined' in a Django project almost always means your urls.py (or the module that builds urlpatterns) is using the path symbol but you never imported it into that file.
Quick checklist to fix it
from django.urls import path, include
If that line is missing, add it.
- If you used url() or re_path() instead of path, import the correct symbol:
from django.urls import re_path # or: from django.conf.urls import url (legacy)
If path is imported but still failing, ensure there is no local variable or function named path shadowing the import.
Confirm Django version: path() was introduced in Django 2.0. On very old Django versions you must use url() or upgrade Django.
Restart the dev server after changes: python manage.py runserver.
Minimal correct examples
Project urls.py (project root)
# project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')), # include your app urls
]
App urls.py (myapp/urls.py)
# myapp/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('items/<int:pk>/', views.detail, name='detail'),
]
If the error appears elsewhere
Traceback location matters. Read the full traceback: it tells you which file and line raised the NameError. Fix that file.
Accidental typo: maybe you wrote Path or pth instead of path.
Imported in wrong file: importing path in one module does not make it available in another.
Debugging tips
print("Loading project.urls")
- In a Python shell inside your project (python manage.py shell) try:
from django.urls import path
print(path)
to confirm path is importable in that environment.
Common related mistakes
Using from django.conf.urls import url and then trying to use path without importing it.
Forgetting include import when using include('app.urls').
Copying example code from a tutorial that uses path but running an older Django release.