Search code examples
androidpythonjythonmonkeyrunner

Trying to get monkeyrunner to include an external .py file


I'm trying to get monkeyrunner to accept an external .py file. The .py file just has some functions in it, nothing fancy.

mainscript.py:

import unittest

import logging
import sys
sys.path.append("C:\path\to\helperscripts\")
from monkeyHelper import monkeyHelper


#log to STDERR
logging.basicConfig(level=logging.DEBUG)

class TestDepthOneFunctions(unittest.TestCase):  

def setUp(self):
    mh = monkeyHelper()
    self.device = mh.setupDevice()
    #monkeyHelper.setupDevice()


def test_myMusic(self):
    self.assertEqual(self.device,3)

def tearDown(self):
    pass

if __name__ == '__main__':
    unittest.main()

monkeyHelper.py:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import logging
import sys


class monkeyHelper(object):

    def __init__(self):
        return

    def setupDevice(self):
        return 3

I've tried just running #>monkeyrunner.bat mainscript.py and it doesn't work. I've seen the full command line that monkeyrunner.bat puts out - is there any way to add C:\path\to\helperScripts to that command line? This is what monkeyrunner.bat spits out:

C:\Windows\system32\java.exe -Xmx512m -Djava.ext.dirs=lib\;lib\x86_64 -Dcom.android.monkeyrunner.bindir=..\framework -jar lib\monkeyrunner.jar mainscript.py

the error i'm getting is:

import monkeyHelper
ImportError: No module named monkeyHelper

I've been banging my head against this all day - any help is appreciated!


Solution

  • Since you can't randomly put \ in a string, try:

    sys.path.append(r"C:\path\to\helperscripts")
    

    The r means that the string is to be treated as a raw string, which means all escape codes will be ignored.

    Also make sure helperMonkey.py is in that path,