Django报错ImportError: cannot import name ‘ugettext_lazy‘ from ‘django.utils.translation‘

Posted cdtaogang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django报错ImportError: cannot import name ‘ugettext_lazy‘ from ‘django.utils.translation‘相关的知识,希望对你有一定的参考价值。

问题描述

博主从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()

解决方法

方法1

降低Django版本为Django==2.2.6

pip install Django==2.2.6 -i https://pypi.tuna.tsinghua.edu.cn/simple/

方法2

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

最后成功启动项目,问题解决

以上是关于Django报错ImportError: cannot import name ‘ugettext_lazy‘ from ‘django.utils.translation‘的主要内容,如果未能解决你的问题,请参考以下文章

Python from django.contrib.auth.views import logout ImportError: cannot import name 'logout'

python3 与 Django 连接数据库报错:ImportError: No module named 'MySQLdb'

Django报错ImportError: cannot import name ‘ugettext_lazy‘ from ‘django.utils.translation‘

Django 运行报错 ImportError: No module named 'PIL'

django 1.8.16报错:ImportError: No module named blog

ImportError: No module named django