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

时间:2018-12-15 14:12:12

标签: python django docker gettext makemessages

我在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

您有什么想法吗?

3 个答案:

答案 0 :(得分:0)

.mo文件可能会引发它。请检查此文件是否为空。 如果确实如此。删除您的.mo文件,然后重新运行makemessages命令。

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

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

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

答案 1 :(得分:0)

就我而言,问题是因为我在 django-admin makemessages 命令上出错。我有一个 msgstr "" 缺失,它被认为是一个致命错误,一旦我添加它,django.mo 文件就会被填满,一切正常。

答案 2 :(得分:-1)

您应该尝试使用:

virtualenv env

source env/bin/activate

然后安装所有要求...