[英]Django- ModuleNotFoundError: No module named 'django.urls'
I am new to python and Django. 我是python和Django的新手。 I am learning using the MDN text guide. 我正在使用MDN文本指南进行学习。 This is what I followed to setup my computer- https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/development_environment#Using_a_virtual_environment 这就是我设置计算机的步骤-https://developer.mozilla.org/zh-CN/docs/Learn/Server-side/Django/development_environment#Using_a_virtual_environment
This is what my command prompt shows me when I try to test my development webserver 这是我尝试测试开发Web服务器时命令提示符向我显示的内容
(my_django_environment2) C:\Users\user\Desktop\django_gopal\mytestsite>py -3 manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000001FEB45AD8C8>
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\django-1.9-py3.6.egg\django\utils\autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "C:\Python36\lib\site-packages\django-1.9-py3.6.egg\django\core\management\commands\runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "C:\Python36\lib\site-packages\django-1.9-py3.6.egg\django\core\management\base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "C:\Python36\lib\site-packages\django-1.9-py3.6.egg\django\core\checks\registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Python36\lib\site-packages\django-1.9-py3.6.egg\django\core\checks\urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "C:\Python36\lib\site-packages\django-1.9-py3.6.egg\django\core\checks\urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Python36\lib\site-packages\django-1.9-py3.6.egg\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python36\lib\site-packages\django-1.9-py3.6.egg\django\core\urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python36\lib\site-packages\django-1.9-py3.6.egg\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python36\lib\site-packages\django-1.9-py3.6.egg\django\core\urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\user\Desktop\django_gopal\mytestsite\mytestsite\urls.py", line 17, in <module>
from django.urls import path
ModuleNotFoundError: No module named 'django.urls'
urls.py urls.py
"""mytestsite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
I did get the development server to work initially but after following the 2nd part of the MDN tutorial-( https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website ) and trying to test my skeleton website is when I first started getting this error. 我确实让开发服务器开始工作,但是遵循了MDN教程的第二部分-( https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website )并尝试测试我的骨架网站是我第一次开始收到此错误的时间。
At this point I had django 1.9 but after reviewing this question-( ModuleNotFoundError: No module named 'django.urls' ), I upgraded to django 2.2.The environment variable and project were both created after upgrading. 这时我有了django 1.9,但在检查了这个问题之后-( ModuleNotFoundError:没有名为'django.urls'的模块 ),我升级到django 2.2。环境变量和项目都是在升级后创建的。 I still however, continued to get the same error. 但是,我仍然继续遇到相同的错误。 I then tried to build a new test site by following the first part(MDN text guide) again but this time with a different virtual environment but again got the same error. 然后,我尝试通过再次遵循第一部分(MDN文本指南)来构建新的测试站点,但是这次是在不同的虚拟环境中进行的,但是再次遇到了相同的错误。
Any help is appreciated 任何帮助表示赞赏
It looks as if py -3
is not using your virtual environment. 看起来py -3
没有使用您的虚拟环境。 Once you have activated the virtual environment, you can simply use python
: 激活虚拟环境后,只需使用python
:
python manage.py runserver
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.