Python 如何解决ImportError: cannot import name simplejson in Django
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27855731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve the ImportError: cannot import name simplejson in Django
提问by Sungpah Lee
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.
我正在尝试在 Django(1.7.1) 中构建一个实时聊天应用程序。看来我需要安装Redis和ishout.js。所以我按照说明安装了它们。
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. And I put the command like
下的MIDDLEWARE_CLASSES
,因为它在说什么。我把命令像
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.
我在 Django 官网搜索后,发现 simplejson 不再使用,并从新的 Django 中删除。我不知道为什么会这样。请提供有关此问题的任何反馈以及解决此问题的可能补救措施。
采纳答案by alecxe
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
,除..
回答by Burhan Khalid
This is a bug in the application itself; unfortunately the error still persists in the master branchat git.
这是应用程序本身的一个错误;不幸的是,该错误仍然存在于git的 master 分支中。
I submitted a pull request to fix the error; in the meanwhile you can do the following:
我提交了一个 pull request 来修复错误;同时,您可以执行以下操作:
pip uninstall django-realtime
pip install git+https://github.com/burhan/django-realtime.git@import-fix
回答by Frank Fang
I think the above answers are workarounds.
我认为上述答案是解决方法。
Django used to ship with simplejsonin django.utils, but this was removed in Django 1.5because jsonmodule 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 方法的地方进行必要的更改。