ImportError: No module named dtmilano
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.
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.
#!/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
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.
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.