【问题标题】:installing simplejson on the google appengine在 google appengine 上安装 simplejson
【发布时间】:2010-04-26 07:02:42
【问题描述】:

超级小问题时间!我正在尝试在 google appengine 上使用 simplejson。在我机器上的终端中,我安装了 simplejson 并且可以正常工作。但是当我尝试将它导入到 appengine 上运行的脚本中时,我收到一条错误消息,指出不存在这样的库。如果在我的机器上打开交互式控制台(来自http://localhost:8080/_ah/admin 上的链接)并输入“import simplejson”,我会得到:

Traceback(最近一次调用最后一次): 文件“/home/chris/google_appengine/google/appengine/ext/admin/init.py”,第 210 行,在帖子中 执行(编译代码,全局()) 文件“”,第 1 行,在 ImportError: 没有名为 simplejson 的模块

有什么想法吗?

【问题讨论】:

  • 没关系,这里是:from django.utils import simplejson

标签: python google-app-engine simplejson


【解决方案1】:

查看django包:

from django.utils import simplejson as json
obj = json.loads(json_string)

由于 Sdk 1.4.2 可以使用以下语句导入 Json:

import simplejson

请注意,在 Python 2.7 运行时您可以使用 native Json library

【讨论】:

  • 我经常看到这个问题的答案,但是如果用户不使用 Django,从 django 包中导入任何东西对我来说似乎有点奇怪。非 Django 用户是否应该直接将通用 simplejson 添加到他们的项目中?还是谷歌在 django 包中提供的版本以某种方式更好地调整到 GAE,然后是 simplejson 的通用版本?由于我大量使用 json,我想确保我使用的是最好的版本。
  • Django 是 SDK 的一部分,所以我认为使用它没有问题。你也可以在你的项目目录中放入 simplejson 并直接使用它。请记住,c 加速不起作用,App Engine 不允许用 c 编写的 python 扩展
  • 我没有具体看Django是如何打包simplejson的,但是只导入部分Django往往会导致导入all的Django。我不知道这里是否是这种情况,但如果是这样,您最好在项目目录中使用 simplejson。
【解决方案2】:

您不再需要在 Google App Engine 上为 simplejson 使用 django 包。

import simplejson as json

这对于避免在日志文件中出现关于 django 版本的大量警告非常方便。

【讨论】:

  • 请注意,对于 python 2.7,您必须使用 json,因为 simplejson 是 c 加速的。
  • 感谢@nmat,您的解决方案完美地帮助了我:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多