0

Hi I am new to python development. I am trying to execute the code given at http://dtmilano.blogspot.in/2012/02/monkeyrunner-interacting-with-views.html but when ever i am trying to execute the code i get following error:

Traceback (most recent call last):
  File "C:\Users\gur31265\workspace\MonkeyRunnerForSmartRecorder\com\test\Runner.py", line 23, in <module>
    from com.dtmilano.android.viewclient import ViewClient
ImportError: No module named dtmilano

I am using eclipse with PyDev and Jython 2.5.3. I had also configured Python 32 on eclipse running on Windows 7 machine. Other Python scripts are running fine but i don't know why code given on dtmilano's blog is causing this error. I had also installed AndroidViewClient and set the ANDROID_VIEW_CLIENT_HOME in system path. Please help.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
Ashwani Kumar
  • 834
  • 3
  • 16
  • 30
  • What's the `sys.path` you get? – Pierre GM Sep 10 '12 at 12:43
  • C:\\Users\\gur31265\\workspace\\MonkeyRunnerForSmartRecorder\\com\\test, C:\\Users\\gur31265\\workspace\\MonkeyRunnerForSmartRecorder, D:\\E & F Drive Back up\\Program Files\\Android\\android-sdk\\tools\\lib\\monkeyrunner.jar, C:\\Users\\gur31265\\Downloads\\dtmilano-AndroidViewClient-bd98f63\\AndroidViewClient\\src, D:\\E & F Drive Back up\\Program Files\\Android\\android-sdk\\tools\\lib\\jython.jar, C:\\Program Files\\Python32\\DLLs, C:\\Program Files\\Python32\\lib, C:\\Program Files\\Python32, C:\\Program Files\\Python32\\lib\\site-packages, C:\\Windows\\system32\\python32.zip – Ashwani Kumar Sep 11 '12 at 04:38
  • If i run it as python module i get the following error:Traceback (most recent call last): File "C:\Users\gur31265\workspace\MonkeyRunner\com\htc\monkey\runner\MonkeyRunner.py", line 19, in from com.dtmilano.android.viewclient import ViewClient ImportError: No module named dtmilano.android.viewclient – Ashwani Kumar Sep 11 '12 at 08:16
  • jars? You're using jython? The script you link to uses `monkeyrunner` as executable, not Python... – Pierre GM Sep 11 '12 at 08:27
  • I am working on windows 7 and i don't have monkeyrunner.exe if i try to set it as interpreter it eclipse will not accept it. Yes, to do this i need a Linux machine. – Ashwani Kumar Sep 11 '12 at 09:11
  • Now i had switched onto Ubuntu. I did tried to set monkeyrunner as python interpreter but eclipse is giving some errors. Can you help me out with this how to set monkeyrunner as python interpreter. – Ashwani Kumar Sep 11 '12 at 10:05
  • I would start by googling `monkeyrunner` and read the doc. – Pierre GM Sep 11 '12 at 10:36
  • @Pierre GM thanks for your support, I managed to make things work but eclipse intigration is still few miles away but i'll reach there soon. Thank you very much. – Ashwani Kumar Sep 12 '12 at 04:13

3 Answers3

1

This took me a bit to work out. Make sure that you append the AndroidViewClient source directory to your path before you import MonkeyRunner.

The following will fail with ImportError: No module named dtmilano:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
sys.path.append('/path/to/android_view_client_home/src')
from com.dtmilano.android.viewclient import ViewClient

However, it will work if you just switch the order:

sys.path.append('/path/to/android_view_client_home/src')
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.dtmilano.android.viewclient import ViewClient

It seems that once it imports the com package for MonkeyRunner, it will only look in that package for dtmilano. However, if both com packages are in the path before you import anything, it will know to look in both locations.

Dean
  • 8,632
  • 6
  • 45
  • 61
  • This is IT! I've struggled so long! This is the only thing that made it work! Thank you! – EGHDK Apr 19 '13 at 07:38
  • I am trying to run an Monkey runner script test..after doing above i started to get "You should use a 'python' interpreter, not 'monkeyrunner' for this module" ...any suggestion? – CoDe May 01 '14 at 09:43
0

You can find a detailed explanation about how to use PYTHONPATH and ANDROID_VIEW_CLIENT_HOME environment variables from Eclipse and PyDev and also from command line at http://dtmilano.blogspot.ca/2012/09/monkeyrunner-importing-from-pythonpath.html.

Briefly:

#!/usr/bin/env monkeyrunner
import re
import sys
import os
import java

# This must be imported before MonkeyRunner and MonkeyDevice,
# otherwise the import fails.
# PyDev sets PYTHONPATH, use it
try:
    for p in os.environ['PYTHONPATH'].split(':'):
       if not p in sys.path:
          sys.path.append(p)
except:
    pass

try:
    sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass

from com.dtmilano.android.viewclient import ViewClient, View
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

Use monkeyrunner located in android SDK to run your code. For example: to compile a file named help.py use following command: c:>monkeyrunner help.py same command will work on linux environment also.

Ashwani Kumar
  • 834
  • 3
  • 16
  • 30