简体   繁体   中英

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

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.

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..

I think the above answers are workarounds.

Django used to ship with simplejson in django.utils , but this was removed in Django 1.5 because json module being available in Python's standard library.

So you should now import json instead of from django.utils import simplejson , and make necessary changes where simplejson methods are called.

This is a bug in the application itself; unfortunately the error still persists in the master branch at git.

I submitted a pull request to fix the error; in the meanwhile you can do the following:

pip uninstall django-realtime
pip install git+https://github.com/burhan/django-realtime.git@import-fix

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM