运行Django项目服务器时遭遇ImportError导入错误
ImportError: No module named urls问题 Hey there, let's get your Django server up and running smoothly! I can spot exactly what's causing this error.
问题根源
The ImportError: No module named urls pops up because you're trying to use from django.urls import path — but this syntax was added in Django 2.0 and later. Looking at your stack trace, you're using Python 2.7, which only supports Django versions up to 1.11.x. In these older Django releases, the URL routing tools live in a different module entirely.
两种修复方案
You’ve got two clear paths forward, depending on whether you want to stick with your current setup or upgrade to modern tools:
选项1:保留Python 2.7和现有Django版本
Adjust your urls.py to use the older, compatible syntax:
- Replace the line
from django.urls import pathwith:from django.conf.urls import url - Update your URL patterns to use the
url()function (which relies on regular expressions) instead ofpath(). For example:
The# 替换原来的 path('admin/', admin.site.urls) url(r'^admin/', admin.site.urls)r'^admin/'regex matches URLs starting withadmin/, which behaves the same as the modernpath('admin/', ...)for simple cases.
选项2:升级到支持path()的环境
If you want to use the cleaner modern path() syntax, you’ll need to:
- Upgrade to Python 3.6 or higher (Python 2.7 is no longer supported by Django or the broader Python community)
- Install a newer, compatible Django version (like Django 3.2 LTS, a stable long-term support release)
pip install django==3.2
Once you’ve upgraded, your original from django.urls import path line will work exactly as intended.
验证你的Django版本
To confirm which version you’re running, run this command in your terminal:
python -m django --version
你的完整错误栈追踪:
Unhandled exception in thread started by <function wrapper at 0x7f535a380938> Traceback (most recent call last): File "/home/kamal/.local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper fn(*args, **kwargs) File "/home/kamal/.local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run self.check(display_num_errors=True) File "/home/kamal/.local/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check include_deployment_checks=include_deployment_checks, File "/home/kamal/.local/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks new_errors = check(app_configs=app_configs) File "/home/kamal/.local/lib/python2.7/site-packages/django/core/checks/urls.py", line 10, in check_url_config return check_resolver(resolver) File "/home/kamal/.local/lib/python2.7/site-packages/django/core/checks/urls.py", line 19, in check_resolver for pattern in resolver.url_patterns: File "/home/kamal/.local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/home/kamal/.local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "/home/kamal/.local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/home/kamal/.local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module return import_module(self.urlconf_name) File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/home/kamal/Desktop/mywebsite/mywebsite/urls.py", line 17, in <module> from django.urls import path ImportError: No module named urls
内容的提问来源于stack exchange,提问作者kamalnath L P

