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

背景

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

python 复制代码
python manage.py migrate
python manage.py runserver

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

python 复制代码
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

python 复制代码
        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
相关推荐
萌萌站起10 小时前
Vscode 中 python模块的导入问题
ide·vscode·python
是小蟹呀^10 小时前
【总结】提示词工程
python·llm·prompt·agent
YBAdvanceFu10 小时前
从零构建智能体:深入理解 ReAct Plan Solve Reflection 三大经典范式
人工智能·python·机器学习·数据挖掘·多智能体·智能体
王夏奇10 小时前
python中的__all__ 具体用法
java·前端·python
王夏奇10 小时前
pycharm中3种不同类型的python文件
ide·python·pycharm
小陈的进阶之路11 小时前
Selenium 滑动 vs Appium 滑动
python·selenium·测试工具·appium
Mike_66611 小时前
txt_json和xml_json
xml·python·json
zyq99101_111 小时前
DFS算法实战:经典例题代码解析
python·算法·蓝桥杯·深度优先
数据知道11 小时前
claw-code 源码分析:从 TypeScript 心智到 Python/Rust——跨栈移植时类型、边界与错误模型怎么对齐?
python·ai·rust·typescript·claude code·claw code
hhh3u3u3u11 小时前
Visual C++ 6.0中文版安装包下载教程及win11安装教程
java·c语言·开发语言·c++·python·c#·vc-1