run .py file in sikuli

Asked by Anusha

Hi,

Is it possible to run a python script (.py file) from sikuli?

for example:
import sys
sys.path.append('D:\Sikuli')
run(python helloworld.py)

Is there something wrong with my syntax or there is some other way?
I'm getting this error:
[error] SyntaxError ( "no viable alternative at input 'helloworld'", )

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
RaiMan (raimund-hocke) said :
#1

# does not help anything for the called Python - obsolete
sys.path.append('D:\Sikuli')

# the parameter for run() must be a string
run("python helloworld.py")

it might be necessary, to use an absolute path for the helloworld.py script, so Python can find it

Be aware of the string conventions with \:
- either use r"...." (no trailing \ )
- or double the \

e.g.
sys.path.append(r'D:\Sikuli') #or
sys.path.append('D:\\Sikuli')

standard Python knowledge ;-)

Revision history for this message
Anusha (md-anusha) said :
#2

Oops! I'm sorry! I'm a total newbie to Sikuli and Python.. Please bear with me..!

Now I've tried this:

import sys
sys.path.append(r'C:\Python27')
run("python helloworld.py")

But I got this error:
[error] ResourceLoaderBasic: runcmd: fatal error: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified

What might be the problem?

Revision history for this message
RaiMan (raimund-hocke) said :
#3

Supposing you have Python 2.7 installed on your machine (I guess in c:\Python27), then you should check, wether this path is on the system path at runtime. If this is not the case, then simply saying python cannot work.

The easiest check:
in a commandline window evaluate, what you have to enter, to get your helloworld.py running.
this then is the command you have to use as the string for run().

again:
neither of this is needed in a SikuliX script:
import sys # never needed - already done internally
sys.path.append(r'C:\Python27') # not relevant for SikuliX and useless for Python run by run()

so simply this should work:
run("python helloworld.py")

BTW: why are you doing this?

Revision history for this message
Anusha (md-anusha) said :
#4

I read your answer in another question and did this:

App.open("Python Shell")
App.focus("Python Shell") # use a suitable part of the command window title
wait(2)
type("execfile(\"helloworld.py\")"+Key.ENTER)
wait (2)

It worked :D
But i have to keep the python shell open before i run this script on sikuli.\

Why am i doing this:
Since I cannot use PyVISA in sikuli, I thought I could write the GUI automation part of the script in Sikuli and the device communication part in python since i can import pyvisa in python (assumption), and from sikuli i can call the python files when ever required.

What do u think?

Revision history for this message
RaiMan (raimund-hocke) said :
#5

Not the appropriate solution for your problem.

Just tested on my Win10 - this works:

- having Python installed in C:\Python27
- having a hello.py in c:\Python\hello.py

# SikuliX script:
python = r"C:\Python27"
pyscript = r"C:\Python\hello.py"
cmd = "%s %s" % (python, pyscript)
result = run(cmd)
print result

# script hello.py
print "hello world"

running the script you get as output in the IDE message area:
0
hello world

where the 1st line is the return code and the following lines are the output produced by hello.py

Revision history for this message
Anusha (md-anusha) said :
#6

Seems like an awesome solution, but i tried that and i got this error :(

[error] ResourceLoaderBasic: runcmd: fatal error: Cannot run program "C:\Python27": CreateProcess error=5, Access is denied
*** error ***
Cannot run program "C:\Python27": CreateProcess error=5, Access is denied

Revision history for this message
Anusha (md-anusha) said :
#7

Im using windows 7

Revision history for this message
RaiMan (raimund-hocke) said :
#8

LOL, so YOU did not pass the test ;-)

... I packed a bug into the script (but not by intention - believe me ;-)

# SikuliX script:
python = r"C:\Python27\python"
pyscript = r"C:\Python\hello.py"
cmd = "%s %s" % (python, pyscript)
result = run(cmd)
print result

Revision history for this message
Anusha (md-anusha) said :
#9

Haha! Very clever and mean of you.. Making fun of beginners :P

You packed another small bug this time as well.. But I figured it out! :P

It's working now!! :D Thank you soo much!

Okay.. One final question (hopefully). What do you think of this? :

I need to automate the testing process of an application.
So I'm using Sikuli for the GUI automation part and Python to use PyVISA and communicate with the device. And then will call the python scripts from the sikuli scripts.

Do you think this is a good approach?

Revision history for this message
RaiMan (raimund-hocke) said :
#10

Congrats !! one step further to scripting championship ;-)

... You packed another small bug this time as well
which one ???

... Do you think this is a good approach?
Yes it is. The Python scripts start up with very little delay and you might directly inspect the resulting output to stdout, that is produced by your Python script (content of result in the above sample).
On top you might exit the Python script with a return code, that is mentioned in the first line of result.

this is how you might access the content of result in your SikuliX script:

resultLines = result.split("\n") # get the output as list of strings - one per line
returnCode = int(resultLines[0].strip()) # get the return code as number

# scan the output lines
for line in resultLines[1:]:
    print line.strip()

the function strip() removes leading and trailing whitespace from the given string

All the best

Revision history for this message
Anusha (md-anusha) said :
#11

Thanks RaiMan, that solved my question.

Revision history for this message
Anusha (md-anusha) said :
#12

Thank you so much!! Thanks to you I don't have to break my head anymore.

Revision history for this message
Anusha (md-anusha) said :
#13

oh and the small error was, in line - pyscript = r"C:\Python\hello.py", intead of python, it worked when I replaced it with Python27. Just a small thing.

Revision history for this message
RaiMan (raimund-hocke) said :
#14

This indeed was by intention: By convention one should not have user content in application folders (Python27 in this case)
So I set up a folder called Python (might better be called MyPythonScripts ;-)

All the best. Always welcome.

Revision history for this message
Anusha (md-anusha) said :
#15

Hello RaiMan,

I've come back with another question.

Instead of calling a python script as a whole from sikuli, is it possible to call a particular function of that python script and also pass arguments while calling?

How is it done?

Revision history for this message
RaiMan (raimund-hocke) said :
#16

This is the standard import concept with P(J)ython:

# have a myModule.py on sys.path
import myModule

returnValue = myModule.aFunction(parm1, parm2)

# myModule.py contains
def aFunction(p1, p2):
     # do something
     return rValue

Revision history for this message
Anusha (md-anusha) said :
#17

The issue is the function I want to import into sikuli consists of pyvisa. I mean, I'm importing pyvisa in the python code i want to use in sikuli. So if I import it is throwing an error 'no module name pyvisa'.

Revision history for this message
Best RaiMan (raimund-hocke) said :
#18

I guess pyvisa is C-based Python stuff, that cannot be used with Jython (SikuliX).

you either have to call Python to run the script in a subprocess or look for a feature compatible package that is Java-based.

Revision history for this message
Anusha (md-anusha) said :
#19

Thanks RaiMan, that solved my question.

Revision history for this message
sohail (safdar) said :
#20

Thanks for your really productive discussion , @ RaiMan i have used your code successfully
 python = r"C:\Python27\python"
pyscript = r"C:\Python\hello.py"
cmd = "%s %s" % (python, pyscript)
 run(cmd)

I have automated GUI part in sikuli then I have automated web part in python using selenium and called that pyhton file using your above described code. Its works fine.

But problem is this Sikuli IDE keeping running, its does not terminate. any solution.

Revision history for this message
RaiMan (raimund-hocke) said :
#21

The Sikuli IDE is interactive and there is no script command to terminate the IDE.

If you have successfully tested such constructs in the IDE, then you have to run your stuff from commandline then (see the docs for that)