繁体   English   中英

Django makemessages“struct.error:解包需要 4 个字节的缓冲区”

[英]Django makemessages "struct.error: unpack requires a buffer of 4 bytes"

我有一个 django 实例打包在 docker-compose 的 Docker 容器中。

我正在尝试为项目生成 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

你有什么想法吗?

它可能由你的 .mo 文件引发。 请检查此文件是否为空。 如果真是这样的话。 删除您的 .mo 文件并重新运行makemessages命令。

它应该可以解决您遇到的问题。

如果这不能解决您的问题。 试试这个:

  1. 将所有 .po 文件保存在不同的位置(创建语言环境文件的备份)
  2. 删除 locale 文件夹中的所有语言。
  3. 重新运行命令

在我的例子中,问题是因为我在django-admin makemessages命令上有错误。 我有一个msgstr ""丢失并且它被提示为致命错误,一旦我添加它django.mo文件被填充并且一切正常。

我在 docker-compose 的 Docker 容器中有一个 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

你有什么想法吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM