how to run a sikuli script in jython.exe or jython IDE

Asked by xiaocezi on 2017-11-20

Hi,

      i have a question about "how to run sikuli script in jython.exe or jython IDE"
i'm not sure it's whether can be achieved,if could achieve in this way ,please let me know how ?
i have worked with sikulix1.1.1 for 2 months and learned a lot.
but still new for the sikuli and python.
thanks very much.

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
xiaocezi
Solved:
2017-12-11
Last query:
2017-12-11
Last reply:
2017-11-20
xiaocezi (xiaocezi) said : #1

i have readed the answer about"How we import sikuli.Sikuli.* module in python"
the page is https://answers.launchpad.net/sikuli/+question/136170
i add path in os environment like:
set JYTHONPATH=c:\Program Files\Sikuli\sikulix.jar\Lib
set CLASSPATH=c:\Program Files\Sikuli\sikulix.jar
set SIKULI="C:\Program Files\Sikuli\sikulix.jar"

but,the sikuli script in jython still not work.

xiaocezi (xiaocezi) said : #2

I've achieved to run SikuliX from Command Line in pycharm with relative path,like :
----------
import os
os.system("runsikulix.cmd -r MonkeyTest-WindowsSystem")
----------
my os is window 7.0.
and i want to know how could i run sikuli code ,like:
----------
click(“x.png”)
----------
in pycharm with python script,but a sikuli script。

i am still study in this continue.

xiaocezi (xiaocezi) said : #4

thank you very very much,Raiman!
You are a good man!
it is suprise for me!
I'll try this way in the link,and reply weather i work with it.

RaiMan (raimund-hocke) said : #6

I am sorry to say, that your solution in comment #5 is a possibility, but not the recommended way for SikuliX.
So I take the right to hide your comment, so others might not be confused.

Still the recommended way for any Jython environment

- have sikulixapi.jar on the Java classpath at runtime

- at the beginning of your script say:
import org.sikuli.script.SikulixForJython
from sikuli import *

So your "wrong" script would shrink to:

import org.sikuli.script.SikulixForJython
from sikuli import *
print "Hello World!Jython"
x=1
print x
click("1m.png")

... except the first 2 lines the same as in the SikuliX IDE - which was the goal for the solution.

xiaocezi (xiaocezi) said : #7

sorry,haha
thank you ,RaiMan,i had tried the scripts that in comment 6,it's work now!
In fact, I did a similar attempt before I did it correctly, maybe I had set up a wrong place before.Now i know how to let it work currently!

xiaocezi (xiaocezi) said : #8

It is puzzling that when i tried this method with jython interpreter in eclipse like:
import org.sikuli.script.SikulixForJython
from sikuli import *

print "Hello World!Jython"
x=1
print x

any = Screen(0)

any.click("1m.png")

the console show:
Traceback (most recent call last):
  File "D:\eclipse-workspace\HelloWorldInJython\jythonhll.py", line 5, in <module>
    import org.sikuli.script.SikulixForJython
ImportError: No module named sikuli

xiaocezi (xiaocezi) said : #9

i must to add [Add source folder] with "C:\\Users\\xie\\AppData\\Roaming\\Sikulix\\Lib" and[ Add zip/jar/egg] sikuliapi.jar for the project,in project->properties ->PyDev-PYTHONPATH->External Libraries page。
and then write the script like:
------------
from sikuli import *

print "Hello World!Jython"
x=1
print x

any = Screen(0)

any.click("1m.png")

------------
i don't know why it doesn't work in comment 8.
maybe something is wrong?

RaiMan (raimund-hocke) said : #11

-- to comment #8:
The recommended solution from comment #6 needs (clearly mentioned)

- have sikulixapi.jar on the Java classpath at runtime

meaning you have to add a
CLASSPATH=<path-to-sikulixapi.jar>

to the jython interpreter's runtime environment.

this is apparently not the case in comment #8

RaiMan (raimund-hocke) said : #12

-- to comment#9:
obviously a possible solution tailored for Eclipse/PyDev

again: the recommended way is one that ALWAYS works for running scripts in any Jython environment with minimum effort and clutter.

xiaocezi (xiaocezi) said : #13

 yes, i agreen with you about "works for running scripts in any Jython environment with minimum effort and clutter."
 i sure again that i had set
CLASSPATH=C:\jython2.7.0\jython.jar;D:\Sikuli\sikulixapi.jar;
and this setting is good working in pycharm,it's strange not work in eclipse with jython.

in my computer's environment variable,BUT it still show in console:
------------------------------
Hello World!Jython
1
Traceback (most recent call last):
  File "D:\eclipse-workspace\Jythonagain\sikuli.py", line 9, in <module>
    from sikuli import *
  File "D:\eclipse-workspace\Jythonagain\sikuli.py", line 16, in <module>
    any = Screen(0)
NameError: name 'Screen' is not defined
------------------------------
thanks and sorry for have you time with this little question.

RaiMan (raimund-hocke) said : #14

You have to somehow add the CLASSPATH inside Eclipse to your projects run environment.

As far as I remember: Eclipse does not automatically propagate the outside (system) environment to the project run environment.
But I do not want to deal with Eclipse.

I am dedicated to the JetBrains products.

BTW: it is not a good idea and not necessary, to have Jython on the classpath. This is handled by jython itself and by the IDE's .

I started to doc the handling of SikuliX scripts in PyCharm:
http://sikulix-2014.readthedocs.io/en/latest/scenarios.html#scripting-with-intellij-pycharm-and-idea-with-python-plugin