`error:unpack在使用syncdb进行迁移时需要一个长度为4的字符串参数错误

时间:2017-09-25 14:25:42

标签: django python-2.7

从未见过类似的东西。当我在Ubuntu终端上的Django应用程序中python manage.py syncdb时,我看到了:

error: unpack requires a string argument of length 4

我不确定这意味着什么或如何解决它。

>     Traceback (most recent call last):
>       File "/home/ubuntu/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/core/management/base.py",
> line 222, in run_from_argv
>         self.execute(*args, **options.__dict__)
>       File "/home/ubuntu/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/core/management/base.py",
> line 250, in execute
>         translation.activate('en-us')
>       File "/home/ubuntu/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/utils/translation/__init__.py",
> line 90, in activate
>         return _trans.activate(language)
>       File "/home/ubuntu/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py",
> line 183, in activate
>         _active.value = translation(language)
>       File "/home/ubuntu/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py",
> line 172, in translation
>         default_translation = _fetch(settings.LANGUAGE_CODE)
>       File "/home/ubuntu/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py",
> line 133, in _fetch
>         res = _translation(globalpath)
>       File "/home/ubuntu/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py",
> line 127, in _translation
>         t = gettext_module.translation('django', path, [loc], DjangoTranslation)
>       File "/usr/lib/python2.7/gettext.py", line 493, in translation
>         t = _translations.setdefault(key, class_(fp))
>       File "/home/ubuntu/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py",
> line 75, in __init__
>         gettext_module.GNUTranslations.__init__(self, *args, **kw)
>       File "/usr/lib/python2.7/gettext.py", line 180, in __init__
>         self._parse(fp)
>       File "/usr/lib/python2.7/gettext.py", line 273, in _parse
>         magic = unpack('<I', buf[:4])[0]
>     error: unpack requires a string argument of length 4

manage.py只包含:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

1 个答案:

答案 0 :(得分:1)

根据追溯(感谢:)),问题源于Django加载其翻译文件(Gettext .mo文件)。

由于magic = unpack('<I', buf[:4])[0]错误出现“error:unpack需要长度为4的字符串参数”,buf可能短于4个字节。这可能(这里只是一个假设)意味着Django尝试加载和失败的地方有一个截断的.mo文件。

我建议尝试重新创建项目的virtualenv,如果这是一个选项 - 或者作为创可贴,你可以暂时禁用Django的i18n位。 (USE_I18N = False