【问题标题】:simplejson error with Django 1.8.2 and dumpdataDjango 1.8.2 和转储数据的 simplejson 错误
【发布时间】:2015-05-24 03:19:13
【问题描述】:

命令:

% ./manage.py dumpdata

输出:

CommandError: Unable to serialize database: cannot import name simplejson

zc.buildout 配置为在 app 目录下安装 simplejson。 simplejson 也存在于我的自定义 Python 目录 /usr/local/python 中。

感谢您的帮助!

堆栈跟踪:

% ./manage.py dumpdata --traceback
Traceback (most recent call last):
  File "./manage.py", line 25, in <module>
    sys.exit(djangorecipe.manage.main('project.settings.settings_dev'))
  File "/opt/project/eggs/djangorecipe-1.11-py2.7.egg/djangorecipe/manage.py", line 9, in main
    management.execute_from_command_line(sys.argv)
  File "/opt/project/eggs/Django-1.8.2-py2.7.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/opt/project/eggs/Django-1.8.2-py2.7.egg/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/project/eggs/Django-1.8.2-py2.7.egg/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/project/eggs/Django-1.8.2-py2.7.egg/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/opt/project/eggs/Django-1.8.2-py2.7.egg/django/core/management/commands/dumpdata.py", line 162, in handle
    stream=stream or self.stdout)
  File "/opt/project/eggs/Django-1.8.2-py2.7.egg/django/core/serializers/__init__.py", line 128, in serialize
    s = get_serializer(format)()
  File "/opt/project/eggs/Django-1.8.2-py2.7.egg/django/core/serializers/__init__.py", line 51, in __call__
    raise self.exception
ImportError: cannot import name simplejson

【问题讨论】:

    标签: python django buildout manage.py dumpdata


    【解决方案1】:

    django.utils.simplejson,不要与simplejson模块混淆,在django 1.7中是deprecated,我们现在依赖python的内置json。 如果您正在运行遗留的第三方代码,或者一些难以更改的代码,请执行此操作

    try:
        import django.utils.simplejson
    except:
        import json as simplejson
    

    如果您正在编写新代码,请编写以下代码以实现向后兼容性:

    try:
        import json
    except:
        import django.utils.simplejson as json
    

    在您的情况下,是 manage.py 抛出错误还是代码中其他地方的错误?你可以发布堆栈跟踪吗?

    【讨论】:

    • 我添加了堆栈跟踪。该错误是由 manage.py 引起的,而不是由应用程序代码引起的。这里的 manage.py 是 buildout 的 bin/django 的链接。
    猜你喜欢
    • 2011-09-21
    • 1970-01-01
    • 2011-12-08
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2012-06-12
    相关资源
    最近更新 更多