Django项目重命名app为article后遇ModuleNotFoundError求简易方案
Hey there, let's get this sorted out quickly—no need to rebuild the whole project! You've already done the core steps, but there are a few small, easy-to-miss things that might be causing this error. Here's a straightforward checklist to fix it:
1. Double-Check Your apps.py Configuration
First, make sure your article/apps.py has the correct name value—this is what Django uses to identify the app internally:
# article/apps.py from django.apps import AppConfig class ArticleConfig(AppConfig): name = 'article' # This must match the new app name exactly
If this was still set to block, Django will keep looking for the old module name.
2. Replace All References to block in Your Codebase
Do a quick search across your entire project for any leftover mentions of block and replace them with article:
- Project URLs: In
forum/urls.py, update anyinclude('block.urls')orfrom block import viewslines - URL Namespaces: If you used namespaces like
{% url 'block:post_detail' %}in templates, change them to{% url 'article:post_detail' %} - Model/View Imports: Check other apps (if any) for lines like
from block.models import Postand update tofrom article.models import Post
3. Clear Python Cache Files
Old cache files can hold references to the old app name. Delete all __pycache__ folders in your project:
# Run this from your project root (where manage.py lives) find . -name "__pycache__" -type d -exec rm -rf {} +
Also, if you're using an IDE like PyCharm or VS Code, restart it—sometimes IDEs cache module paths even after you've renamed files.
4. Restart the Development Server (Properly)
Django's auto-reloader can sometimes hang onto old module data. Stop the server completely with Ctrl+C, then restart it:
python manage.py runserver
5. Verify Migrations (Optional)
Since you already deleted and recreated migrations, this is probably fine, but just to be safe:
python manage.py makemigrations article python manage.py migrate
That's it! These steps should resolve the module not found error without any complex workarounds.
内容的提问来源于stack exchange,提问作者Wizard

