2

我在将一个看似现有的模块:simplejson 导入到我的 MonkeyRunner 脚本中时遇到了一些困难。

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import simplejson

def main():
    print "this is a test"

if __name__ == "__main__":
    main()

ImportError:没有名为 simplejson 的模块

据我了解,MonkeyRunner 使用基于 Python 2.5 的 Jython 2.5?。我知道 JSON 模块来自 Python 2.7,但我已经在“/Library/Python/2.5/site-packages/simplejson-2.3.2-py2.5-macosx-10.7-x86_64.egg”下安装了 Python 2.5 的 simplejson

我的问题是,如何正确地将 simplejson 模块导入 MonkeyRunner 脚本?

4

1 回答 1

2

Installing a package for Python does not make it available for use with Jython.

Jython is based on Python (aka CPython) in the sense that the former aims to be compatible with the latter, but the implementations are quite different.

It is possible to add the CPython version of simplejson to Jython's path:

import sys
sys.path.append("/Library/Python/2.5/site-packages/simplejson-2.3.2-py2.5-macosx-10.7-x86_64.egg")
import simplejson

This "trick" happens to work (for me at least...) with the simplejson package. With other packages it won't work at all.

I would prefer to actually install simplejson for Jython. See How can I install various Python libraries in Jython? for details.

于 2012-01-29T09:28:21.710 回答