简体   繁体   English

无法从 django.urls 导入路径

[英]Unable to import path from django.urls

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 行,在 ImportError 中:无法导入名称“路径”

I am using django version 1.11我正在使用 django 版本 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 .您无法导入路径的原因是因为它是 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.如果您这样做,您将看到1.11文档中没有path条目。

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

Use url instead of path.使用 url 而不是路径。

from django.conf.urls import url

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: Python 2 不支持 Django 2。在 Mac 上安装 Python 3 和 Django 2 后,从 shell 运行以下命令以在保留路径的同时运行您的应用程序:

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

The version of Django will then be wrong and you will see import errors for path然后Django的版本会出错,你会看到path导入错误

For those who are using python 2.7, python2.7 don't support django 2 so you can't install django.urls.对于那些使用 python 2.7 的人,python2.7 不支持 django 2 所以你不能安装 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 3.6,那么您需要将 django 升级到大于 2 的最新版本。

  • On PowerShell在 PowerShell 上

    pip install -U django 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.下一个提示出现时,这意味着它现在已安装并可以使用。

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??确保安装了Django?? any another packages?还有其他包吗? ie 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我希望这有帮助

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

I changed the python interpreter and it worked.我更改了 python 解释器并且它起作用了。 On the keyboard, I pressed ctrl+shift+p.在键盘上,我按下了 ctrl+shift+p。 On the next window, I typed python: select interpreter, and there was an option to select the interpreter I wanted.在下一个窗口中,我输入 python: select interpreter,然后有一个选项可以选择我想要的解释器。 From here, I chose the python interpreter located in my virtual environment.从这里,我选择了位于我的虚拟环境中的 python 解释器。
In this case, it was my ~\\DevFolder\\myenv\\scripts\\python.exe在这种情况下,它是我的~\\DevFolder\\myenv\\scripts\\python.exe

看起来好像你忘记激活你的虚拟环境尝试运行python3 -m venv venv或者如果你已经设置了虚拟环境尝试通过运行source venv/bin/activate

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您也可以尝试通过以下方式安装 django-urls

pipenv install django-urls

As error shows that path can not be imported.由于错误显示无法导入路径。

在此处输入图片说明

So here we will use the url instead of path as shown below:-所以在这里我们将使用url而不是路径,如下所示:-

first import the url package then replace the path with 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 .有关更多信息,您可以参考此链接

Create setting.json file in your project在您的项目中创建 setting.json 文件

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

it'simple : 1-go to the view on the vscode 2-choose command palette 3-write "select interpreter" and choose the suitable python version .很简单:1-转到 vscode 上的视图 2-选择命令面板 3-编写“选择解释器”并选择合适的 Python 版本。

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.你能做的最好的事情就是升级你当前使用的 django 版本,因为以前的版本不支持路径。

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.如果您安装了两个版本的 Python,例如 Python 3.9.6 和 Python 3.10.7,并且如果您使用的是 visual studio code(vscode),请导航到右下角,您会看到您正在使用的版本,然后更改为另一个版本。 see attached screenshot.请参阅随附的屏幕截图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM