博主从GitHub
上拉取下了一个Django
项目,该项目依赖环境为Django==2.2.6
版本,而博主本地Anaconda3
环境下的Django==4.1.3
版本,在终端输入python manage.py runserver
启动项目后,提示错误:ImportError: cannot import name ‘ugettext_lazy’ from ‘django.utils.translation’ (D:\ProgramData\Anaconda3\lib\site-packages\django\utils\translation_init_.py)
from django.utils.translation import ugettext_lazy as _
ImportError: cannot import name 'ugettext_lazy' from 'django.utils.translation' (D:\ProgramData\Anaconda3\lib\site-packages\django\utils\translation\__init__.py)
“ugettext_lazy
”已被 Django 3+
弃用,因此您将无法将其与 Django
版本 >= 3
一起使用 https://code.djangoproject.com/ticket/30165
Along the lines of #27753 (Cleanups when no supported version of Django supports Python 2 anymore), the legacy functions in -- , , , , and -- are simple aliases that remain for Python 2 Unicode backwards compatibility. As other compatibility layers have been cleaned up, these shims can be deprecated for removal.django.utils.translationugettext()ugettext_lazy()ugettext_noop()ungettext()ungettext_lazy()
降低Django版本为Django==2.2.6
pip install Django==2.2.6 -i https://pypi.tuna.tsinghua.edu.cn/simple/
I believe you are using Django 4. ugettext_lazy has been removed in Django 4.0 415. Please use gettext_lazy instead:
# from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _
解决以上错误后,还会提示其他ImportError如:
错误1:ImportError: cannot import name ‘force_unicode’ from ‘django.utils.encoding’ (D:\ProgramData\Anaconda3\lib\site-packages\django\utils\encoding.py)
# from django.utils.encoding import force_unicode as force_text # noqa
from django.utils.encoding import force_str as force_text # noqa
错误2:ImportError: cannot import name ‘ungettext’ from ‘django.utils.translation’ (D:\ProgramData\Anaconda3\lib\site-packages\django\utils\translation_init_.py)
# from django.utils.translation import ungettext, ugettext as _
from django.utils.translation import gettext_lazy, gettext_lazy as _
错误3:ImportError: cannot import name ‘render_to_response’ from ‘django.shortcuts’ (D:\ProgramData\Anaconda3\lib\site-packages\django\shortcuts.py)
# from django.shortcuts import render_to_response
from django.shortcuts import render