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
相关推荐
抱抱宝16 分钟前
Pyecharts之特殊图表的独特展示
python·信息可视化·数据分析
deephub1 小时前
Python GIL(全局解释器锁)机制对多线程性能影响的深度分析
python·机器学习·gil
MatpyMaster2 小时前
基于PyQt5打造的实用工具——PDF文件加图片水印,可调大小位置,可批量处理!
python·pdf
go54631584652 小时前
python 从知网的期刊导航页面抓取与农业科技相关的数据
开发语言·python·科技
米码收割机2 小时前
【python】tkinter实现音乐播放器(源码+音频文件)【独一无二】
开发语言·python·pygame
星如雨グッ!(๑•̀ㅂ•́)و✧3 小时前
Java NIO全面详解
java·python·nio
笛柳戏初雪3 小时前
Python中的函数(下)
开发语言·python
码界筑梦坊3 小时前
基于Django的个人博客系统的设计与实现
后端·python·django·毕业设计
weixin_307779133 小时前
AWS EMR上的Spark日志实时搜索关键指标网页呈现的设计和实现
大数据·python·spark·云计算·aws