简体   繁体   English

无法导入“django.urls”

[英]Unable to import 'django.urls'

Help please.请帮忙。 am new to django please i have installed django in virtual environment i created on my desktop.我是 django 的新手,请我在我的桌面上创建的虚拟环境中安装了 django。 but whenever i create a django project and open up the folder in vs code in the url.py file and the wsgi.py files there is a red underline in the import statements.但是每当我创建 django 项目并在 url.py 文件和 wsgi.py 文件中的 vs 代码中打开文件夹时,导入语句中都会出现红色下划线。 and when i hover over it it says unable to import the package name当我 hover 超过它时,它说无法导入 package 名称

this is from the wsgi.py file.这是来自 wsgi.py 文件。 it says unable to import django.core.wsgi i have not written any code so there are no errors yet but vscode shows a red underline on the from import statements它说无法导入 django.core.wsgi 我还没有编写任何代码,所以还没有错误,但是 vscode 在 from import 语句上显示红色下划线

from django.core.wsgi import get_wsgi_application

also in the urls.py file it does the same thing在 urls.py 文件中它也做同样的事情

from django.contrib import admin
from django.urls import path

i have tried uninstalling and installing django again but it didn't work.我已经尝试再次卸载和安装 django 但它没有用。 it still does the samething它仍然做同样的事情

ps.附言。 i have not written any code in the django project prior to this but no doubt this will surely give me an error在此之前我没有在 django 项目中编写任何代码,但毫无疑问,这肯定会给我一个错误

This has to do with your starting project because Django can't import the initial files.这与您的起始项目有关,因为 Django 无法导入初始文件。 You're doing something wrong to install, without a doubt.毫无疑问,你做错了安装。

Make sure to install Django within the virtual env that you've created and use the Django-admin start project command to install Django确保在您创建的虚拟环境中安装 Django 并使用 Django-admin 启动项目命令安装 Django

virtualenv projectname

Activate virtualenv激活虚拟环境

Next to your folder for virtualenv write in command在您的 virtualenv 文件夹旁边写入命令

pip install Django. 

Note the dot at the end, required to start a project within the folder you're currently in but next to your virtualenv folder请注意末尾的点,在您当前所在的文件夹中启动项目所需,但在您的 virtualenv 文件夹旁边

Now it should install Django within env现在它应该在 env 中安装 Django

Next, run接下来,运行

python manage.py check

If all okay如果一切顺利

python manage.py runserver

Also check your naming of project还要检查您的项目命名

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

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