Django1.8+Python3.5.2部署生产环境时遇‘No module named django.urls’错误求助
Hey there! Let's get this sorted out quickly. The root cause here is straightforward: django.urls didn't exist in Django 1.8 — it was introduced in Django 1.10. Chances are your code was written with a newer Django version in mind, or you accidentally used modern syntax that's incompatible with 1.8. Here's how to fix it step by step:
1. Update Your Import Statements
Go through every file in your project (especially urls.py files and any views/settings that reference URL utilities) and replace any imports from django.urls with Django 1.8-compatible alternatives:
- Replace
from django.urls import path, includewithfrom django.conf.urls import url, include - Replace
from django.urls import reverse, reverse_lazywithfrom django.core.urlresolvers import reverse, reverse_lazy
Example URL Conf Conversion
If your original urls.py looked like this (modern Django syntax):
from django.urls import path, include from . import views urlpatterns = [ path('', views.home, name='home'), path('accounts/', include('accounts.urls')), ]
Change it to this for Django 1.8:
from django.conf.urls import url, include from . import views urlpatterns = [ url(r'^$', views.home, name='home'), # Uses regex instead of path strings url(r'^accounts/', include('accounts.urls')), ]
Note that Django 1.8 uses url() with regular expressions instead of the simpler path() function from newer versions.
2. Verify Your Django Version on Linux
Double-check that your Linux server is actually running Django 1.8 (it's easy to mix up virtual environments!). Run this command in your project's environment:
pip show django
Look for the Version line — if it's not 1.8.x, uninstall the wrong version and install the correct one:
pip uninstall django -y pip install django==1.8
3. Clean Up Cached Files
Linux can hang onto old cached Python files that might cause conflicts. Delete all __pycache__ directories in your project:
find /path/to/your/project -name "__pycache__" -type d -exec rm -r {} +
Then restart your web server (Apache, Nginx + Gunicorn, etc.) to ensure fresh code is loaded.
4. Double-Check SQLite Permissions
Since you're using SQLite instead of PostgreSQL, make sure the database file (db.sqlite3) has the right permissions for your web server process to read/write it:
chmod 644 /path/to/your/project/db.sqlite3 chmod 755 /path/to/your/project # Ensure the parent directory is accessible
That should resolve the "No module named django.urls" error. If you run into any other snags, feel free to share more details about specific files or error messages!
内容的提问来源于stack exchange,提问作者Abhijeet Arora

