Search code examples
androidpythonmodulemonkeyrunner

How does Python (or MonkeyRunner) locate imported modules?


Update: Following @dtmilano's suggestion, I added

import sys
print(sys.path)

to the beginning of my MonkeyRunner script. This results in

['e:/path/android-sdk/tools/lib/monkeyrunner.jar:e:\\path\\bbct\\android\\functional-tests', 'E:\\path\\android-sdk\\tools\\lib\\Lib', '/E:/path/android-sdk/tools/lib/jython-standalone-2.5.3.jar/Lib', '__classpath__', '__pyclasspath__/']

At first glance I thought this included the current working directory. However, a closer inspection showed that the output is a list of strings where the first string is

'e:/path/android-sdk/tools/lib/monkeyrunner.jar:e:\\path\\bbct\\android\\functional-tests'

For some reason this contains two paths concatenated together. Is this a bug in MonekyRunner and/or Jython?

Original Question: I have two .py files in the same directory: screenshots.py and util.py. I need to run screenshots.py with the monkeyrunner interpreter from the Android build tools. When I run monkeyrunner screenshots.py, I get error No module named util. How do I configure my python and/or monkeyrunner to find the code in util.py?

Edit: I am using Git Bash on Win7. (Oops, I probably should have mentioned this earlier.)

For reference, this is the complete error message:

130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "c:\Users\Dell\Documents\dev\src\java\bbct\android\functional-tests\screenshots.py", line 19, in
import util
ImportError: No module named util

130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.Py.ImportError(Py.java:264)
130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.imp.import_first(imp.java:657) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.imp.import_name(imp.java:741) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.imp.importName(imp.java:791) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.ImportFunction.call(_builtin_.java:1236) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyObject.call(PyObject.java:367) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.builtin._import_(builtin.java:1207) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.builtin._import_(builtin.java:1190) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.imp.importOne(imp.java:802) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.pycode._pyx0.f$0(c:\Users\Dell\Documents\dev\src\java\bbct\android\functional-tests\screenshots.py:51) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.pycode._pyx0.call_function(c:\Users\Dell\Documents\dev\src\java\bbct\android\functional-tests\screenshots.py) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyTableCode.call(PyTableCode.java:165) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.PyCode.call(PyCode.java:18) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.Py.runCode(Py.java:1197) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.core.builtin.execfile_flags(builtin.java:538) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:156) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at com.android.monkeyrunner.ScriptRunner.run(ScriptRunner.java:116) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at com.android.monkeyrunner.MonkeyRunnerStarter.run(MonkeyRunnerStarter.java:77) 130807 12:01:59.978:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]
at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:189)


Solution

  • For your reference, the android SDK does appear to have a bug in it where the first item on sys.path is the monkeyRunner lib and the working directory mashed together. I added the following to fix.

    import sys
    sys.path.append(sys.path[0].split(':',1)[1])
    import util