56

Tried to run command:

from django.urls import path

Getting error:

Traceback (most recent call last): File "< stdin >", line 1, in ImportError: cannot import name 'path'

I am using django version 1.11

Lev
  • 999
  • 2
  • 10
  • 26

16 Answers16

53

The reason you cannot import path is because it is new in Django 2.0 as is mentioned here: https://docs.djangoproject.com/en/2.0/ref/urls/#path.

On that page in the bottom right hand corner you can change the documentation version to the version that you have installed. If you do this you will see that there is no entry for path on the 1.11 docs.

Nick Chapman
  • 4,402
  • 1
  • 27
  • 41
31

You need Django version 2

pip install --upgrade django
pip3 install --upgrade django

python -m django --version # 2.0.2
python3 -m django --version # 2.0.2
jasonleonhard
  • 12,047
  • 89
  • 66
27

Use url instead of path.

from django.conf.urls import url

urlpatterns = [
    url('', views.homepageview, name='home')
]
Miron
  • 1,007
  • 2
  • 17
  • 31
Saurabh Shukla
  • 458
  • 6
  • 8
  • What would be impact if use url instead of path !!, Any idea ? – CoDe Jun 18 '18 at 01:20
  • 2
    Don't do this! From the Django 2.1 docs - `This function is an alias to django.urls.re_path(). It’s likely to be deprecated in a future release.` – alex Nov 15 '18 at 16:56
8

Python 2 doesn't support Django 2. On a Mac once you've installed Python 3 and Django 2 run the following command from shell to run your app while keeping path:

python3 manage.py runserver

Even if you have upgraded and are on a mac you will, by default, run Python 2 if you're entering the following command:

python manage.py runserver

The version of Django will then be wrong and you will see import errors for path

Lydia Thomas
  • 93
  • 1
  • 5
  • Hi Lydia, thanks for contributing but that wasn't my issue, the issue was with me following a tutorial for version 2.0 and having Django 1.11 installed. To your answer, it's possible to either uninstall the default osx python or associate python with python with 3.x instead of 2.0 – Lev Oct 25 '18 at 15:59
4

I changed the python interpreter and it worked. On the keyboard, I pressed ctrl+shift+p. On the next window, I typed python: select interpreter, and there was an option to select the interpreter I wanted. From here, I chose the python interpreter located in my virtual environment.
In this case, it was my ~\DevFolder\myenv\scripts\python.exe

RKRK
  • 1,284
  • 5
  • 14
  • 18
3

For those who are using python 2.7, python2.7 don't support django 2 so you can't install django.urls. If you are already using python 3.6 so you need to upgrade django to latest version which is greater than 2.

  • On PowerShell

    pip install -U django

  • Verification

>

PS C:\Users\xyz> python
Python 3.6.6 |Anaconda, Inc.| (default, Jul 25 2018, 15:27:00) [MSC v.1910 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> from django.urls import path
>>>

As next prompt came, it means it is installed now and ready to use.

LOrD_ARaGOrN
  • 3,884
  • 3
  • 27
  • 49
3

My assumption you already have settings on your urls.py

from django.urls import path, include 
# and probably something like this 
    urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
]

and on your app you should have something like this blog/urls.py

 from django.urls import path

 from .views import HomePageView, CreateBlogView

 urlpatterns = [
   path('', HomePageView.as_view(), name='home'),
   path('post/', CreateBlogView.as_view(), name='add_blog')
 ]

if it's the case then most likely you haven't activated your environment try the following to activate your environment first pipenv shell if you still get the same error try this methods below

make sure Django is installed?? any another packages? i.e pillow try the following

pipenv install django==2.1.5 pillow==5.4.1

then remember to activate your environment

pipenv shell

after the environment is activated try running

python3 manage.py makemigrations

python3 manage.py migrate

then you will need to run

python3 manage.py runserver

I hope this helps

user3719458
  • 346
  • 3
  • 12
2

How to use url both app(pages) and in the project.

entire project url configuration root/urls.py

 from django.conf.urls import url, include
 from django.contrib import admin
 urlpatterns = [
     url(r'^admin/', admin.site.urls),
     url('', include('pages.urls')),
   ]

app pages url configuration root/pages/urls.py

# pages/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url('', views.homePageView, name='home')
]
1

It look's as if you forgot to activate you virtual environment try running python3 -m venv venv or if you already have virtual environment set up try to activate it by running source venv/bin/activate

user3719458
  • 346
  • 3
  • 12
1

For someone having the same problem -

import name 'path' from 'django.urls' 
(C:\Python38\lib\site-packages\django\urls\__init__.py)

You can also try installing django-urls by

pipenv install django-urls
Shoejep
  • 4,414
  • 4
  • 22
  • 26
0

As error shows that path can not be imported.

enter image description here

So here we will use the url instead of path as shown below:-

first import the url package then replace the path with url

from django.conf.urls import url
urlpatterns = [
    url('admin/', admin.site.urls),
]

for more information you can take the reference of this link.

Anoop Kumar
  • 845
  • 1
  • 8
  • 19
0

Create setting.json file in your project

{
       "python.pythonPath": "${workspaceFolder}/env/bin/python3",
       "editor.formatOnSave": true,
       "python.linting.pep8Enabled": true,
       "python.linting.pylintPath": "pylint",
       "python.linting.pylintArgs": ["--load-plugins", "pylint_django"],
       "python.linting.pylintEnabled": true,
       "python.venvPath": "${workspaceFolder}/env/bin/python3",
       "python.linting.pep8Args": ["--ignore=E501"],
       "files.exclude": {
           "**/*.pyc": true
       }
}
sshashank124
  • 31,495
  • 9
  • 67
  • 76
Mary
  • 11
  • 2
0

it'simple : 1-go to the view on the vscode 2-choose command palette 3-write "select interpreter" and choose the suitable python version .

it's useful for me :)

Farouk Mhamdi
  • 311
  • 2
  • 8
0

The best thing you can do is to do an upgrade of the django version that you're currently using as the previous doesn't support path.

0

If you have installed two versions of Python, for instance, Python 3.9.6 and Python 3.10.7 and if you are using visual studio code(vscode), navigate to the bottom right-hand corner where you see the version you are using and change to another version. see attached screenshot.

Kimz21
  • 1
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 15 '22 at 12:50
0

I have faced the same issue and for me this worked,

from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('')
]