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

一曲冷凌霜 提交于 2019-12-09 14:49:27

问题


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.


回答1:


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




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/27855731/how-to-solve-the-importerror-cannot-import-name-simplejson-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!