Python异常处理TypeError: translation() got an unexpected keyword argument ‘codeset‘

2024-01-01 17:33:06

背景

学习graphql-python安装好依赖后执行命令

python manage.py migrate
python manage.py runserver

仅接着出现下列错误,主要提示是 「TypeError: translation() got an unexpected keyword argument ‘codeset’」

ntribute_to_class
    self.remote_field.through = create_many_to_many_intermediary_model(self, cls)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/software_1/miniconda3/envs/learnLangchain/lib/python3.11/site-packages/django/db/models/fields/related.py", line 1051, in create_many_to_many_intermediary_model
    'verbose_name': _('%(from)s-%(to)s relationship') % {'from': from_, 'to': to},
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
  File "/Users/software_1/miniconda3/envs/learnLangchain/lib/python3.11/site-packages/django/utils/functional.py", line 149, in __mod__
    return str(self) % rhs
           ^^^^^^^^^
  File "/Users/software_1/miniconda3/envs/learnLangchain/lib/python3.11/site-packages/django/utils/functional.py", line 113, in __text_cast
    return func(*self.__args, **self.__kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/software_1/miniconda3/envs/learnLangchain/lib/python3.11/site-packages/django/utils/translation/__init__.py", line 75, in gettext
    return _trans.gettext(message)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/software_1/miniconda3/envs/learnLangchain/lib/python3.11/site-packages/django/utils/translation/trans_real.py", line 286, in gettext
    _default = _default or translation(settings.LANGUAGE_CODE)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/software_1/miniconda3/envs/learnLangchain/lib/python3.11/site-packages/django/utils/translation/trans_real.py", line 199, in translation
    _translations[language] = DjangoTranslation(language)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/software_1/miniconda3/envs/learnLangchain/lib/python3.11/site-packages/django/utils/translation/trans_real.py", line 90, in __init__
    self._init_translation_catalog()
  File "/Users/software_1/miniconda3/envs/learnLangchain/lib/python3.11/site-packages/django/utils/translation/trans_real.py", line 131, in _init_translation_catalog
    translation = self._new_gnu_trans(localedir)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/software_1/miniconda3/envs/learnLangchain/lib/python3.11/site-packages/django/utils/translation/trans_real.py", line 119, in _new_gnu_trans
    return gettext_module.translation(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: translation() got an unexpected keyword argument 'codeset'



        return gettext_module.translation(
            domain=self.domain,
            localedir=localedir,
            languages=[self.__locale],
            codeset='utf-8',
            fallback=use_null_fallback,
        )



分析与解决

根据异常栈定位到源代码位置 /Users/software_1/miniconda3/envs/learnLangchain/lib/python3.11/site-packages/django/utils/translation/trans_real.py,代码调用python方法传入参数codeset

        return gettext_module.translation(
            domain=self.domain,
            localedir=localedir,
            languages=[self.__locale],
            codeset='utf-8',
            fallback=use_null_fallback,
        )

当前python版本为3.11,源码查看并没有该参数。随后看了教程文章,前置依赖要求是python3.6。 接着看了一下Django版本为2.1.4,所以是版本间冲突导致。

解决:最终有两种解决方式,升级Django版本或者切换python版本

  1. 查看Django最新版依赖要求,升级到最新版解决问题。
  2. 使用python3.6

文章来源:https://blog.csdn.net/Revivedsun/article/details/135326577
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。