From django.urls import path ImportError: cannot import name 'path' from 'django.urls'

I am very beginner in Python. I have project in Python 3.8 and Django. When I try update my old Django to Django 1.11 i have error:

** from django.urls import path ImportError: cannot import name 'path' from 'django.urls' (/home/ds/.local/lib/python3.9/site-packages/django/urls/init.py)**

My file:

from django.urls import path
from django.contrib.auth.views import login
from .views import *

urlpatterns = ['',
    path(r'^$', 'go.insurance.views.index'),
    path(r'^abc/', include('go.abc.urls')),
    path(r'^help/', include('go.help.urls')),

    path(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt')),
]

if settings.DEBUG:

    urlpatterns += ['',
        path(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
    ]

What i make:

  1. update: requirements/base.txt => Django==1.11.29
  2. /mnt/c/Project/.venvs/bin/python3 -m pip install --upgrade pip
  3. /mnt/c/Project/.venvs/bin/pip3 install django==1.11
  4. /.venvs/bin/python3 manage.py runserver in folder requrements

When i run my app i have error: ./.venvs/bin/python3 manage.py runserver

I have error:


Exception ignored in thread started by: <function check_errors.<locals>.wrapper at 0x7fdc2c9b91f0>
Traceback (most recent call last):
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    self.check(display_num_errors=True)
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/core/management/base.py", line 356, in check
    all_issues = self._run_checks(
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/mnt/c/Project/go/urls.py", line 8, in <module>
    from django.urls import path
ImportError: cannot import name 'path' from 'django.urls' (/mnt/c/Project/.venvs/lib/python3.9/site-packages/django/urls/__init__.py)

How can i repair it? Pleasae help me :)

The error message you’re seeing indicates that Python is unable to import path from django.urls. This is because the path function does not exist in Django 1.11. It was introduced in Django 2.0.

In Django 1.11, you should use url instead of path. So, you need to change path to url in your urlpatterns. Here’s an example:

from django.conf.urls import url
from django.contrib.auth.views import login
from .views import *

urlpatterns = [
    url(r'^$', 'go.insurance.views.index'),
    url(r'^abc/', include('go.abc.urls')),
    url(r'^help/', include('go.help.urls')),

    url(r'^robots\.txt$', TemplateView.as_view(template_name='robots.txt')),
]

if settings.DEBUG:
    urlpatterns += [
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
    ]

For the second problem: The error you’re encountering is because in Django’s url function, you need to pass in a callable view function, not a string. In your code, 'go.insurance.views.index' is a string, not a function. You need to import this view function first, and then use it directly in the url function.

from django.conf.urls import url
from go.insurance.views import index

urlpatterns = [
    url(r'^$', index),
    # other url patterns...
]
Back to Top