无法在Django中导入simplejson

无法在Django中导入simplejson

本文介绍了如何解决ImportError:无法在Django中导入simplejson的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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 

但是我仍然有这个导入错误消息:

 最近的电话最后):
文件manage.py,第10行,在< module>
execute_from_command_line(sys.argv)
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py,行385,在execute_from_command_line
utility.execute()
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__ .py,行354,执行
django.setup()
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/__init__ .py,第21行,设置
apps.populate(settings.INSTALLED_APPS)
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ django / apps / registry.py,第85行,填入
app_config = AppConfig.create(entry)
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /site-packages/django/apps/config.py,第87行,创建
module = import_module(entry)
文件/Library/Frameworks/Python.framework/Ver sions / 2.7 / lib / python2.7 / importlib / __ init__.py,第37行,import_module
__import __(name)
文件/Library/Frameworks/Python.framework/Versions/2.7/lib /python2.7/site-packages/drealtime/__init__.py,第4行在< module>
从django.utils import simplejson as json

在我通过Django官方网站进行搜索之后,我发现simplejson不再被使用,并从新的Django中删除。我不知道为什么会发生这种情况。
请给出关于这个问题的任何反馈和可能的补救办法来解决这个问题。

解决方案

你使用的是过时的版本的 django-realtime



将其升级到最新版本,他们:

  pip install django-realtime --upgrade 
/ pre>

如果错误仍然存​​在,请直接从github安装主分支:

  $ pip install git + https://github.com/anishmenon/django-realtime.git --upgrade 

FYI,修复:

  try:
from django.utils import simplejson as json
除了:
import simplejson as json

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.

解决方案

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

这篇关于如何解决ImportError:无法在Django中导入simplejson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:49