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
相关推荐
databook12 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室13 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三14 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271118 小时前
Python之语言特点
python
刘立军18 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机1 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i1 天前
django中的FBV 和 CBV
python·django
c8i1 天前
python中的闭包和装饰器
python
这里有鱼汤1 天前
小白必看:QMT里的miniQMT入门教程
后端·python