[英]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). 我正在尝试在Django(1.7.1)中构建一个实时聊天应用程序。 It seems that I needed to install Redis and ishout.js. 看来我需要安装Redis和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: 在Django中创建项目后,我将'drealtime'放在INSTALLED_APPS下,然后放入:
'drealtime.middleware.iShoutCookieMiddleware'
right above : 正上方 :
'django.contrib.sessions.middleware.SessionMiddleware'
under the MIDDLEWARE_CLASSES
as it was saying. 正如它所说的那样在MIDDLEWARE_CLASSES
之下。 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. 在我搜索了Django官方网站后,我发现simplejson已不再使用并从新的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
. 您正在使用过时的django-realtime
版本。
Upgrade it to the latest version, they fixed the 1.7 compatibility : 升级到最新版本,他们修复了1.7兼容性 :
pip install django-realtime --upgrade
If the error persists, install directly from github, master branch: 如果错误仍然存在,请直接从github,master分支安装:
$ 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.. 裸例外条款-禅程序员里面是杀害我耳语except ImportError
, except ImportError
, 除了..
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. Django曾经在django.utils中发布了simplejson ,但是在Django 1.5中删除了它,因为json模块在Python的标准库中可用。
So you should now import json
instead of from django.utils import simplejson
, and make necessary changes where simplejson methods are called. 所以你现在应该import json
而不是from django.utils import simplejson
,并在调用simplejson方法时进行必要的更改。
This is a bug in the application itself; 这是应用程序本身的一个错误; unfortunately the error still persists in the master branch at git. 不幸的是,错误仍然存在于 git 的master分支中。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.