Django makemessages "struct.error: unpack requires a buffer of 4 bytes" (Django makemessages“struct.error:unpack需要4个字节的缓冲区”)

7

我有一个在docker-compose中打包的Django实例。

我正在尝试为项目生成makemessages文件,但是当我尝试运行makemessages时,我收到了以下响应:

root@6fc510c9c5d1:/code# python manage.py makemessages
/usr/local/lib/python3.6/dist-packages/daphne/server.py:12: UserWarning: Something has already installed a non-asyncio Twisted reactor. Attempting to uninstall it; you can fix this warning by importing daphne.server early in your codebase or finding the package that imports Twisted and importing it later on.
  UserWarning,
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/dist-packages/django/core/management/__init__.py", line 347, in execute
    django.setup()
  File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.6/dist-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/local/lib/python3.6/dist-packages/django/contrib/auth/models.py", line 94, in <module>
    class Group(models.Model):
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 152, in __new__
    new_class.add_to_class(obj_name, obj)
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 315, in add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/local/lib/python3.6/dist-packages/django/db/models/fields/related.py", line 1586, in contribute_to_class
    self.remote_field.through = create_many_to_many_intermediary_model(self, cls)
  File "/usr/local/lib/python3.6/dist-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 "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 148, in __mod__
    return str(self) % rhs
  File "/usr/local/lib/python3.6/dist-packages/django/utils/functional.py", line 112, in __text_cast
    return func(*self.__args, **self.__kw)
  File "/usr/local/lib/python3.6/dist-packages/django/utils/translation/__init__.py", line 77, in gettext
    return _trans.gettext(message)
  File "/usr/local/lib/python3.6/dist-packages/django/utils/translation/trans_real.py", line 312, in gettext
    _default = _default or translation(settings.LANGUAGE_CODE)
  File "/usr/local/lib/python3.6/dist-packages/django/utils/translation/trans_real.py", line 221, in translation
    _translations[language] = DjangoTranslation(language)
  File "/usr/local/lib/python3.6/dist-packages/django/utils/translation/trans_real.py", line 122, in __init__
    self._add_installed_apps_translations()
  File "/usr/local/lib/python3.6/dist-packages/django/utils/translation/trans_real.py", line 170, in _add_installed_apps_translations
    translation = self._new_gnu_trans(localedir)
  File "/usr/local/lib/python3.6/dist-packages/django/utils/translation/trans_real.py", line 149, in _new_gnu_trans
    fallback=use_null_fallback)
  File "/usr/lib/python3.6/gettext.py", line 538, in translation
    t = _translations.setdefault(key, class_(fp))
  File "/usr/lib/python3.6/gettext.py", line 254, in __init__
    self._parse(fp)
  File "/usr/lib/python3.6/gettext.py", line 345, in _parse
    magic = unpack('<I', buf[:4])[0]
struct.error: unpack requires a buffer of 4 bytes

你有任何想法吗?它可能是什么?


您的问题出现在此行代码:magic = unpack('<I', buf[:4])[0],并出现以下错误:struct.error: unpack requires a buffer of 4 bytes - Benyamin Jafari
你能否在这里放上相关的代码片段和提到的错误? - Benyamin Jafari
2个回答

9
可能是由于您的.mo文件引起的。请检查此文件是否为空。 如果确实是这种情况,请删除您的.mo文件并重新运行makemessages命令。
这应该解决您遇到的问题。
如果这不解决您的问题,请尝试以下操作:
  1. 将所有.po文件保存在另一个位置(创建区域设置文件的备份)
  2. 删除locale文件夹中的所有语言。
  3. 重新运行命令

0
在我的情况下,问题是因为我在django-admin makemessages命令上出现了错误。我缺少了一个msgstr "",它被提示为致命错误,一旦我添加了它,django.mo文件就被填充了,一切都正常工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接