Unable to import path from django.urls

PythonDjangoPython 3.xDjango Views

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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

Solution 3 - Python

Use url instead of path.

from django.conf.urls import url

urlpatterns = [    url('', views.homepageview, name='home')]

Solution 4 - Python

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

Solution 5 - Python

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.

Solution 6 - Python

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

Solution 7 - Python

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

Solution 8 - Python

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')
]

Solution 9 - Python

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

Solution 10 - Python

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

Solution 11 - Python

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.

Solution 12 - Python

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
       }
}

Solution 13 - Python

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 :)

Solution 14 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLevView Question on Stackoverflow
Solution 1 - PythonNick ChapmanView Answer on Stackoverflow
Solution 2 - PythonjasonleonhardView Answer on Stackoverflow
Solution 3 - PythonSaurabh ShuklaView Answer on Stackoverflow
Solution 4 - PythonLydia ThomasView Answer on Stackoverflow
Solution 5 - PythonRishi BansalView Answer on Stackoverflow
Solution 6 - Pythonuser3719458View Answer on Stackoverflow
Solution 7 - PythonJuliusKiuraView Answer on Stackoverflow
Solution 8 - PythonKrishnamoorthy AcharyaView Answer on Stackoverflow
Solution 9 - Pythonuser3719458View Answer on Stackoverflow
Solution 10 - PythonAnupam TirkeyView Answer on Stackoverflow
Solution 11 - PythonAnoop KumarView Answer on Stackoverflow
Solution 12 - PythonMaryView Answer on Stackoverflow
Solution 13 - PythonFarouk MhamdiView Answer on Stackoverflow
Solution 14 - PythonPaul MunyaoView Answer on Stackoverflow