【问题标题】:Python with Django Import error with RegistrationSupplementBase cannot import name 'ugettext_lazy'Python with Django Import error with RegistrationSupplementBase cannot import name 'ugettext_lazy'
【发布时间】:2019-07-18 15:57:34
【问题描述】:

我正在更新一个非常旧的 Django 项目并尝试使用 RegistrationSupplementBase,但在导入时我收到以下错误消息:

File "/home/projectmachine/Desktop/project_rebuild/projectname/models.py", line 11, in <module>
from registration.supplements.base import RegistrationSupplementBase
File "/home/projectmachine/.local/share/virtualenvs/projectname-QrYA9Qp-/lib/python3.6/site-packages/registration/supplements/base.py", line 9, in <module>
    from django.utils.text import ugettext_lazy as _
ImportError: cannot import name 'ugettext_lazy'

我不知道出了什么问题。安装的依赖项似乎存在问题。我正在使用 Django 2.2 和 django-inspectional-registration 0.6.2

这是我导入类的方式:

from registration.supplements.base import RegistrationSupplementBase

【问题讨论】:

  • 这些已移至django.utils.translation。但无论如何,这些都被贬低了。你应该用from django.utils.translation import gettext_lazy as _替换它。
  • 在这种情况下,错误来自 django-inspectional-registration。看起来它有not been updated 好几年了,所以它不支持最新版本的 Django。您可以尝试分叉代码并进行 Willem 建议的更改,但我怀疑需要进行其他更改才能使其与 Django 2.2 一起使用。

标签: python django python-3.6 django-registration


【解决方案1】:

我不知道出了什么问题。安装的依赖项似乎存在问题。我正在使用带有 django-inspectional-registration 0.6.2 的 Django 2.2

该函数已移至 django.utils.translation 模块,因此您可以使用以下命令导入:

from <b>django.utils.translation</b> import ugettext_lazy as _

基于Django Deprecation Timeline [Django-doc]ugettext_lazy 将在 中删除。你可以改用gettext_lazy

from django.utils.translation import <b>gettext_lazy</b> as _

然而,基于GitHub repository of django-inspectional-registration,该项目不再处于活动状态:最新提交是在 2016 年 11 月。您可以尝试更新该项目,但也许最好寻找一个在类似情况下工作的替代包方式。

【讨论】:

  • 感谢您的帮助!我会考虑其他选项。
猜你喜欢
  • 2022-12-28
  • 2020-11-30
  • 1970-01-01
  • 2022-12-28
  • 2015-08-12
  • 2020-07-24
  • 1970-01-01
  • 2023-03-26
  • 2018-11-13
相关资源
最近更新 更多