Unable to import path from django.urls
Question:
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
Answers:
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.
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
Use url instead of path.
from django.conf.urls import url
urlpatterns = [
url('', views.homepageview, name='home')
]
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')
]
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
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:Usersxyz> 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.
As error shows that path can not be imported.
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.
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
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
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 ~DevFoldermyenvscriptspython.exe
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
}
}
For someone having the same problem –
import name 'path' from 'django.urls'
(C:Python38libsite-packagesdjangourls__init__.py)
You can also try installing django-urls by
pipenv install django-urls
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 🙂
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.
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.
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
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.
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
Use url instead of path.
from django.conf.urls import url
urlpatterns = [
url('', views.homepageview, name='home')
]
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')
]
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
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:Usersxyz> 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.
As error shows that path can not be imported.
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.
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
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
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 ~DevFoldermyenvscriptspython.exe
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
}
}
For someone having the same problem –
import name 'path' from 'django.urls'
(C:Python38libsite-packagesdjangourls__init__.py)
You can also try installing django-urls by
pipenv install django-urls
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 🙂
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.
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.