[1.1.4] How to run .py script with Jython?

Asked by Jean-Francois Thuong on 2018-08-31

Hi,

With Sikulix 1.1.1, I was running a python/Jython script will following command line after doing the setup of Sikuli.

===
java -Dsikuli.Debug=-2 -cp sikulix.jar org.python.util.jython main.py
===

With the removal of Setup in Sikuli 1.1.4, I am confused on how to run (and the documentation does not seem updated on this topic).

I have tried:

* `java -cp "sikulix.jar;jython.jar" org.python.util.jython main.py`
* Installing Jython, adding `sys.path.append(".../sikulix.jar")` in my script and running `jython main.py`

They both return the error: **ImportError: No module named sikuli**.

How shall I do?

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Jean-Francois Thuong
Solved:
2018-09-03
Last query:
2018-09-03
Last reply:
2018-09-03
RaiMan (raimund-hocke) said : #1

Follow the instructions on the download page to get the IDE running with Jython.

After that you can use the command the same as before:
java -Dsikuli.Debug=-2 -cp sikulix.jar org.python.util.jython main.py

No need to do anything else.

Hi,

Thanks for the answer

>> Follow the instructions on the download page to get the IDE running with Jython.

Do you mean:
* Downloading jython-standalone.jar
* running java -jar sikulix.jar
?

After doing that, running "java -Dsikuli.Debug=-2 -cp sikulix.jar org.python.util.jython main.py" will lead to following error because org.python.util.jython cannot be found in sikulix.jar.

===
Error: Could not find or load main class org.python.util.jython
===

What am I missing?

PS: For what that matters, this is the script I am running

from sikuli.Sikuli import *

notepad = App('notepad.exe')
notepad.open()
focusWindow = notepad.focusedWindow()
sleep(1)
type("hello!!{ENTER}How are you{SHIFT}?{ENTER}Good!")
notepad.close()

RaiMan (raimund-hocke) said : #3

The sikulix.jar beginning with 1.1.4 does not contain the Jython stuff any more.

The steps as mentioned on the download page lead to a Jython jar being in the SikuliX extension folder, from where it is used by SikuliX IDE or command line scriptruns the normal way (running .sikuli folders).

If you want to run plain .py files with Jython and SikuliX, then you have to follow the setup and use instructions of Jython and finally take care, that sikulix.jar is on the Java classpath at runtime of Jython.

BTW: Why do you use SikuliX this way and not the standard SikuliX usage IDE/commandline?

Another possibility:
java -cp<sikulix>;<jython> org.python.util.jython main.py

... <...> have to be substituted by the real absolute paths to the jars.

Thanks @RaiMan.

>>> BTW: Why do you use SikuliX this way and not the standard SikuliX usage IDE/commandline?

I am used to have Python script alone and it worked so far so ...

I have actually figured out what was missing:

* The script needed an extra "import" before the usual "from sikuli.Sikuli import *"
import org.sikuli.script.SikulixForJython

* Then I could use the command line:
 java -cp<sikulix>;<jython> org.python.util.jython main.py

PS: It worked with just the name of the jar if it is run from current folder

I have entered a question and answer in StackOverflow here:
https://stackoverflow.com/questions/52148058/running-sikulix-1-1-4-python-scripts-from-command-line

Thanks a lot for your time!!

RaiMan (raimund-hocke) said : #5

Might be, that this is a solution for you at this time.

... but it will only work up to Java 8.

With Java 9+
import org.sikuli.script.SikulixForJython

will not do the job anymore (add something to class path at runtime).

I have it on the list to find a convenience solution.

Thanks for the info RaiMan.

How do you run a .sikuli folder with Sikuli 1.1.4 by the way?

RaiMan (raimund-hocke) said : #7

up to 1.1.3:
see docs
https://sikulix-2014.readthedocs.io/en/latest/faq/010-command-line.html

currently with 1.1.4:
instead of
runsikulix(.cmd)

use
java -jar <path-to>/sikulix.jar ... parameter as with 1.1.x