0

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)

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268

3 Answers3

1

You need to add the module to the search path (you are cwd is not in the same dir?)

http://docs.python.org/2/tutorial/modules.html

import sys sys.path.append('/path/to/your/module')

Phuong Nguyen
  • 909
  • 7
  • 20
  • Yes, the `cwd` is the directory that contains the two .py files. – Code-Apprentice Aug 08 '13 at 18:25
  • Specifically [6.1.2 The Module Search Path](http://docs.python.org/2/tutorial/modules.html#the-module-search-path) answers the question in the title of my Q. Unfortunately, it doesn't quite answer the underlying question which is how to fix the problem. I'll be looking into that shortly. – Code-Apprentice Aug 10 '13 at 22:21
1

The directory where the script is located is automatically added to sys.path by monkeyrunner (and this works pretty well for other OSs, unfortunately it seems you are using one where it doesn't). Then other modules present in the same directory of the script can be imported without problems.

That is, screenshots.py should find utils.py because the directory functional-tests is in sys.path.

You can verify its content by doing

import sys
print sys.path
import util

in screenshots.py. My guess is that all those c:\ are messing with the path.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • That makes sense since this worked on my Debian Linux box but doesn't on my Win7 box using Git Bash (basically mingw). – Code-Apprentice Aug 08 '13 at 18:25
  • After adding the code that you suggested here, I see that my CWD is in `sys.path`. Any ideas why I'm still getting the "No module named util" error when running from Git Bash under Win7? – Code-Apprentice Aug 22 '13 at 18:33
  • I've updated my Q with the actual output of `print sys.path`. It doesn't contain the CWD after all. It's concatenated in with another path. Any ideas why and how I can fix it? – Code-Apprentice Sep 19 '13 at 20:28
  • Probably you have to include the directory explicitly – Diego Torres Milano Sep 19 '13 at 21:10
1

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
bryanbcook
  • 16,210
  • 2
  • 40
  • 69