How to solve the ImportError: cannot import name simplejson in Django

Sungpah Lee picture Sungpah Lee · Jan 9, 2015 · Viewed 22k times · Source

I'm trying to build a realtime chat app in Django(1.7.1). It seems that I needed to install Redis and ishout.js. So I installed them by following the instructions.

After making the project in Django, I put 'drealtime' under the INSTALLED_APPS, and put:

'drealtime.middleware.iShoutCookieMiddleware' 

right above :

'django.contrib.sessions.middleware.SessionMiddleware' 

under the MIDDLEWARE_CLASSES as it was saying. And I put the command like

python manage.py startapp example

but still I have this import error message:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/apps/config.py", line 87, in create
    module = import_module(entry)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/drealtime/__init__.py", line 4, in <module>
    from django.utils import simplejson as json

After I searched through the Django official site, I found simplejson is no longer used and removed from new Django. I don't know why this is happening. Please give any feedback on this issue and possible remedy to tackle this issue.

Answer

alecxe picture alecxe · Jan 9, 2015

You are using an outdated version of django-realtime.

Upgrade it to the latest version, they fixed the 1.7 compatibility:

pip install django-realtime --upgrade

If the error persists, install directly from github, master branch:

$ pip install git+https://github.com/anishmenon/django-realtime.git --upgrade

FYI, the fix:

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

Bare exception clause - zen programmer inside is killing me whispering except ImportError, except ImportError, except..