Django- ModuleNotFoundError:没有名为“ django.urls”的模块

时间:2018-08-02 13:08:56

标签: python django

我是python和Django的新手。我正在使用MDN文本指南进行学习。 这就是我设置计算机-https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/development_environment#Using_a_virtual_environment

所遵循的步骤

这是我尝试测试开发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

"""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),
]

我确实使开发服务器开始工作,但是在遵循MDN教程第二部分(https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website)并尝试测试我的骨架网站之后,这是我第一次开始遇到此错误时。

这时我有了django 1.9,但在检查了这个问题(ModuleNotFoundError: No module named 'django.urls')之后,我升级到django 2.2。环境变量和项目都是在升级后创建的。但是,我仍然继续遇到相同的错误。然后,我尝试通过再次遵循第一部分(MDN文本指南)来构建新的测试站点,但是这次是在不同的虚拟环境中进行的,但是再次遇到了相同的错误。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

似乎py -3没有使用您的虚拟环境。激活虚拟环境后,您只需使用python

python manage.py runserver