使用django-extensions的shell_plus时出现ModuleNotFoundError求助
Let's break down what's happening and how to fix this quickly:
Why This Error Occurs
Django 3.0 removed the deprecated django.contrib.admin.templatetags.admin_static module — it was replaced by the standard static templatetag from django.templatetags.static. However, the version of django-extensions you're using (2.2.1) still tries to import this removed module when launching shell_plus, which triggers the error.
The Recommended Fix: Upgrade django-extensions
This is the cleanest and most maintainable solution. Versions of django-extensions 2.2.5 and newer are fully compatible with Django 3.0, and they've fixed the broken reference to admin_static.
Run this command to upgrade:
pip install --upgrade django-extensions>=2.2.5
Once the upgrade finishes, start shell_plus again — it should load all your models automatically without errors:
python manage.py shell_plus
Temporary Workaround (If You Can't Upgrade)
If you're stuck on django-extensions==2.2.1 for specific reasons, you can create a dummy module to resolve the missing import:
- Create this directory structure in your project root (make sure each folder contains an empty
__init__.pyfile to mark it as a Python package):django/contrib/admin/templatetags/ - Inside that folder, create a file named
admin_static.pywith this code:from django.templatetags.static import static from django.utils.deprecation import RemovedInDjango40Warning import warnings warnings.warn( "admin_static is deprecated; use django.templatetags.static instead", RemovedInDjango40Warning, ) __all__ = ['static']
This redirects the old admin_static import to the supported static templatetag, and adds a warning to remind you to upgrade when possible.
Verify the Fix
After applying either solution, run python manage.py shell_plus again. It should now work as expected, with all your models pre-loaded into the shell.
内容的提问来源于stack exchange,提问作者codyc4321




