[英]How to solve the ImportError: cannot import name simplejson in Django
我正在嘗試在Django(1.7.1)中構建一個實時聊天應用程序。 看來我需要安裝Redis和ishout.js。 所以我按照說明安裝了它們。
在Django中創建項目后,我將'drealtime'放在INSTALLED_APPS下,然后放入:
'drealtime.middleware.iShoutCookieMiddleware'
正上方 :
'django.contrib.sessions.middleware.SessionMiddleware'
正如它所說的那樣在MIDDLEWARE_CLASSES
之下。 我把命令就像
python manage.py startapp example
但我仍然有這個導入錯誤消息:
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
在我搜索了Django官方網站后,我發現simplejson已不再使用並從新的Django中移除。 我不知道為什么會這樣。 請提供有關此問題的任何反饋以及解決此問題的可能補救措施。
您正在使用過時的django-realtime
版本。
升級到最新版本,他們修復了1.7兼容性 :
pip install django-realtime --upgrade
如果錯誤仍然存在,請直接從github,master分支安裝:
$ pip install git+https://github.com/anishmenon/django-realtime.git --upgrade
僅供參考,修復:
try:
from django.utils import simplejson as json
except:
import simplejson as json
裸例外條款-禪程序員里面是殺害我耳語except ImportError
, except ImportError
, 除了..
我認為以上答案是解決方法。
Django曾經在django.utils中發布了simplejson ,但是在Django 1.5中刪除了它,因為json模塊在Python的標准庫中可用。
所以你現在應該import json
而不是from django.utils import simplejson
,並在調用simplejson方法時進行必要的更改。
這是應用程序本身的一個錯誤; 不幸的是,錯誤仍然存在於 git 的master分支中。
我提交了一個拉取請求來修復錯誤; 同時你可以做到以下幾點:
pip uninstall django-realtime
pip install git+https://github.com/burhan/django-realtime.git@import-fix
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.