simplejson error with Django 1.8.2 and dumpdata

2.1k Views Asked by At

Command:

% ./manage.py dumpdata

Output:

CommandError: Unable to serialize database: cannot import name simplejson

zc.buildout is configured to install simplejson in the app directory. simplejson is also present in my custom Python directory, /usr/local/python.

Thanks for your help!

Stack trace:

% ./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
1

There are 1 best solutions below

1
Sebastian Wozny On

django.utils.simplejson, not to be confused with the simplejson module was deprecated in django 1.7, we now rely on the built-in json of python. If you're running legacy third party code, or some code that's difficult to change do this

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

If you're writing new code, write this for backwards compatiblity:

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

In your case it's the manage.py throwing the error or is the error somewhere else in the code? Can you post the stacktrace?