Python 无法从 django.urls 导入路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47563013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to import path from django.urls
提问by Lev
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'
回溯(最近一次调用最后一次):文件“< stdin >”,第 1 行,在导入错误中:无法导入名称“路径”
I am using django version 1.11
我正在使用 Django 1.11 版
回答by Nick Chapman
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.
您无法导入路径的原因是因为它是 Django 2.0 中的新内容,如下所述: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.
在右下角的那个页面上,您可以将文档版本更改为您已安装的版本。如果你这样做,你会看到文档path
上没有条目1.11
。
回答by jasonleonhard
You need Django version 2
你需要 Django 版本 2
pip install --upgrade django
pip3 install --upgrade django
python -m django --version # 2.0.2
python3 -m django --version # 2.0.2
回答by Saurabh Shukla
Use url instead of path.
使用 url 而不是路径。
from django.conf.urls import url
urlpatterns = [
url('', views.homepageview, name='home')
]
回答by Lydia Thomas
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:
Python 2 不支持 Django 2。在 Mac 上安装 Python 3 和 Django 2 后,从 shell 运行以下命令以运行您的应用程序,同时保留路径:
python3 manage.py runserver
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:
即使您已经升级并且使用的是 mac,如果您输入以下命令,默认情况下也会运行 Python 2:
python manage.py runserver
python manage.py runserver
The version of Django will then be wrong and you will see import errors for path
Django 的版本会出错,你会看到导入错误 path
回答by Krishnamoorthy Acharya
How to use url both app(pages) and in the project.
如何在应用程序(页面)和项目中使用 url。
entire project url configuration root/urls.py
整个项目 url 配置 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
应用页面 url 配置 root/pages/urls.py
# pages/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url('', views.homePageView, name='home')
]
回答by user3719458
My assumption you already have settings on your urls.py
我的假设你已经在你的 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
在你的应用程序上你应该有这样的东西 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
如果是这种情况,那么很可能您还没有激活您的环境pipenv shell
如果您仍然遇到相同的错误,请先尝试以下操作来激活您的环境,请尝试以下方法
make sure Django is installed?? any another packages? i.e pillow try the following
确保安装了Django??还有其他包吗?即枕头尝试以下
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
我希望这有帮助
回答by Rishi Bansal
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.
对于那些使用 python 2.7 的人,python2.7 不支持 django 2 所以你不能安装 django.urls。如果您已经在使用 python 3.6,那么您需要将 django 升级到大于 2 的最新版本。
On PowerShell
pip install -U django
Verification
在 PowerShell 上
pip install -U django
确认
>
>
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.
当下一个提示出现时,这意味着它现在已安装并可以使用。
回答by Anoop Kumar
As error shows that path can not be imported.
由于错误显示无法导入路径。
So here we will use the urlinstead of pathas shown below:-
所以在这里我们将使用url而不是路径,如下所示:-
first import the urlpackage then replace the pathwith url
首先导入url包然后用url替换路径
from django.conf.urls import url
urlpatterns = [
url('admin/', admin.site.urls),
]
for more information you can take the reference of this link.
有关更多信息,您可以参考此链接。
回答by user3719458
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
看起来好像你忘记激活你的虚拟环境尝试运行python3 -m venv venv
或者如果你已经设置了虚拟环境尝试通过运行来激活它 source venv/bin/activate
回答by JuliusKiura
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
我更改了 python 解释器并且它起作用了。在键盘上,我按下了 ctrl+shift+p。在下一个窗口中,我输入 python: select interpreter,然后有一个选项可以选择我想要的解释器。从这里,我选择了位于我的虚拟环境中的 python 解释器。
在这种情况下,这是我的~\DevFolder\myenv\scripts\python.exe