Importing MonkeyRunner into Python script fails in Windows

Jarno Argillander picture Jarno Argillander · Feb 9, 2012 · Viewed 10.6k times · Source

Why importing MonkeyRunner into Python script fails in Windows?

I created a file foo.py and copy-pasted code from the MonkeyRunner docs. The following error appears when compiling the sample code in cmd line:

Traceback (most recent call last):
  File "foo.py", line 2, in 
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
ImportError: No module named com.android.monkeyrunner


The Android tools folder has monkeyrunner.jar... and I fail to understand how to tell Python to import classes from it.


Follow-up

I found out the error came because I didn't have the latest Android tools (16).

  1. Update to newest tools (16) & platform (15) first
  2. Add android-sdk\tools into path
  3. Put foo.py into android-sdk\tools folder (it doesn't seem to work anywhere else)
  4. Switch usb debugging on
  5. Attach phone to PC
  6. Type "monkeyrunner foo.py"

Answer

Ivo picture Ivo · Feb 10, 2012

A) MonkeyRunner uses Jython, so you must make sure you have that installed

B) Jython must know where MonkeyRunner is. You can either have the script at the same place as where MonkeyRunner is located, or you can add it to Jython's path, so Jython will be able to find it no matter where you run the script from.