run .py file in sikuli
Hi,
Is it possible to run a python script (.py file) from sikuli?
for example:
import sys
sys.path.
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:
Revision history for this message
|
#1 |
# does not help anything for the called Python - obsolete
sys.path.
# 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.
sys.path.
standard Python knowledge ;-)
Revision history for this message
|
#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.
run("python helloworld.py")
But I got this error:
[error] ResourceLoaderB
What might be the problem?
Revision history for this message
|
#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.
so simply this should work:
run("python helloworld.py")
BTW: why are you doing this?
Revision history for this message
|
#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(
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
|
#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\
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
|
#6 |
Seems like an awesome solution, but i tried that and i got this error :(
[error] ResourceLoaderB
*** error ***
Cannot run program "C:\Python27": CreateProcess error=5, Access is denied
Revision history for this message
|
#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\
pyscript = r"C:\Python\
cmd = "%s %s" % (python, pyscript)
result = run(cmd)
print result
Revision history for this message
|
#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
|
#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
# 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
|
#11 |
Thanks RaiMan, that solved my question.
Revision history for this message
|
#12 |
Thank you so much!! Thanks to you I don't have to break my head anymore.
Revision history for this message
|
#13 |
oh and the small error was, in line - pyscript = r"C:\Python\
Revision history for this message
|
#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
|
#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
|
#16 |
This is the standard import concept with P(J)ython:
# have a myModule.py on sys.path
import myModule
returnValue = myModule.
# myModule.py contains
def aFunction(p1, p2):
# do something
return rValue
Revision history for this message
|
#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
|
#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
|
#19 |
Thanks RaiMan, that solved my question.
Revision history for this message
|
#20 |
Thanks for your really productive discussion , @ RaiMan i have used your code successfully
python = r"C:\Python27\
pyscript = r"C:\Python\
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
|
#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)