0

I am new to python and Django. I am learning using the MDN text guide. 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

This is what my command prompt shows me when I try to test my development webserver

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

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.

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. 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.

Any help is appreciated

1 Answers1

0

It looks as if py -3 is not using your virtual environment. Once you have activated the virtual environment, you can simply use python:

python manage.py runserver
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Thank you for replying @Alasdair. It works when I use python instead of py -3. Is there a reason for this? The MDN guide says to use py -3 instead of python3 on windows. I'm on windows and when I use python3, it says it doesn't recognise the command. – tigercraft22 Aug 02 '18 at 18:21
  • As I said in the question, my usual advice is to make sure the virtual environment is to activate the virtual environment, then just use `python`. For a Python 3 virtual environment, this will use Python 3. I remember another user having similar problems with `py -3` when following the MDN guide, but I couldn't find the question when I searched for it today. I don't use Python on Windows, so I don't know why `python3` and `py -3` are not working for you, or whether there's a good reason for the MDN guide to recommend `py -3`. – Alasdair Aug 02 '18 at 23:17