2

I am having a bit of difficulty importing a seemingly existing module: simplejson into my MonkeyRunner script.

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

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

if __name__ == "__main__":
    main()

ImportError: No module named simplejson

As I understand it, MonkeyRunner uses Jython 2.5, based on Python 2.5?. I know that the JSON module came in Python 2.7, but I have installed simplejson for Python 2.5 under '/Library/Python/2.5/site-packages/simplejson-2.3.2-py2.5-macosx-10.7-x86_64.egg'

My question is, how do I properly get simplejson modules imported into a MonkeyRunner script?

AaronMT
  • 1,605
  • 2
  • 11
  • 6

1 Answers1

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.

Community
  • 1
  • 1
mzjn
  • 48,958
  • 13
  • 128
  • 248