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

3

12

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.

Transpose answered 9/1, 2015 at 7:25 Comment(0)
E
14

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

Eby answered 9/1, 2015 at 7:29 Comment(7)
Unfortunately, I don't have any view.py file yet because when I tried to make the application, this error popped up, not allowing me to have any view.py file. The init.py file is empty for now.Transpose
@SungpahLee nono, I mean update the django-realtime package, reinstall it to the latest version: pip install django-realtime --upgrade.Eby
rescomp-14-285794:realpro sungpah$ pip install django-realtime --upgrade Requirement already up-to-date: django-realtime in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages Cleaning up... rescomp-14-285794:realpro sungpah$Transpose
@SungpahLee see the updated answer please (install from github).Eby
And still I have a same problem when trying to make an app. When I installed django-realtime, the command was like "pip install django-realtime"Transpose
Awesome. Now the app is installed!!!!!!Thanks so much. I was bogged down to this issue for 6 hours..Transpose
1+ for try..except section, really helpfulPrecipitin
T
5

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.

Titanic answered 10/3, 2015 at 5:47 Comment(0)
V
2

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
Vacuity answered 9/1, 2015 at 7:45 Comment(1)
Cool. Thanks for a really great advice on this issue and hope that the generic problem would be solved quicklyTranspose

© 2022 - 2024 — McMap. All rights reserved.