Importing MonkeyRunner into Python script fails in Windows
Asked Answered
Q

1

10

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"
Queue answered 9/2, 2012 at 10:44 Comment(0)
Y
6

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.

Yoke answered 10/2, 2012 at 3:56 Comment(4)
Thanks. About the Python part: do you know how to import MonkeyRunner in Python code? The doc example is in Python and the "The monkeyrunner API" specifically says so: "In a Python program, you access each class as a Python module."Queue
@JarnoArgillander Jython is a Python interpreter, written in Java. It runs python code, and python programs. Although CPython (the normal Python interpreter) will be able to parse the code, it will never be able to run it, because the MonkeyRunner module is written in Java. Jython translates between the two languages. Trust me, if you want to run MonkeyRunner, you need to install Jython as use that as your Python interpreter.Yoke
I'm going to need your help once more: I tried separately installed Jython, which throws the same error. Tried adding Android tools folder into path and copy-pasting relevant jars into my project folder without any help. Also run the script via monkeyrunner.bat with error: Can't open specified script file. What should I do to run scripts using MonkeyRunner?Queue
@Jarno : are you able to open monkeyrunner shell prompt.?Dorman

© 2022 - 2024 — McMap. All rights reserved.