How to import Sikuli in Jython?

Asked by tlondon

Thanks in advance for the help. I realize there are questions addressing this, however, none of the described potential solutions helped or I misunderstood them.

I'm writing some very basic tests in Jython/Netbeans and would like to get it working but unfortunately I can't seem to get Sikuli imported properly. I've tried unzipping the sikuli-script.jar and placing it in its' own directory, I've tried using sys.path.append, I've tried from sikuli import *, and I've tried putting everything under the sun in my system PATH. None of these seem to work.

Here's a little test program:

#from sikuli import *
import sys
sys.path.append("C:\\sikuli-script\\Lib")

class Login:
   path = "path =C:\\Program Files (x86)\\Program\\prog.exe"
   program = ""

   def __init__(self):
      self.program = ""

   def launch(self):
      program = App.open(path);

demo = Login()
demo.launch()

Traceback (most recent call last):
  File "Test\src\test.py", line 16, in <module>
    demo.launch()
  File "Test\src\test.py", line 13, in launch
    program = App.open(path);
NameError: global name 'App' is not defined

I'm using
Windows 7 64 bit.
JDK 1.7
Jython 2.5.3
Netbeans 7.3

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
Roman Podolyan (podolyan-roman) said :
#1
Revision history for this message
Roman Podolyan (podolyan-roman) said :
#2

It seems that you don't have env variables set up properly. When I ran Sikuli with Jython, I used Jybot.bat, modified with env variables settings :

=====
@echo off

set sikuli_jar=C:\Program Files (x86)\Sikuli X\sikuli-script.jar

set CLASSPATH=%sikuli_jar%
set JYTHONPATH=%sikuli_jar%/Lib

jybot --pythonpath=. ^
      --outputdir=results ^
      --loglevel=TRACE ^
      %*
=====

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

Based on the mentioned configuration I suppose you are using Sikuli-API 1.0.0

Roman's suggestion should work if you want to use jython from command line (besides the fact, that you have to have a ref to Sikuli's libs folder in system path at runtime)

If you want to setup a project in Netbeans 7.3 with the Python plugin, you have to first add a ref to sikuli-script.jar TO THE END of the class path of your used Jython config (project props -> Python tab -> manage ... button)

Second you need a ref to .../sikuli-script.jar/Lib in your sys.path at runtime.

1. option (in the script):
import sys
lib = "... path-to-your... /sikuli-script.jar/Lib"
sys.path.append(lib)
from sikuli import *
popup("hallo")

2. option (copy folder .../sikuli-script.jar/Lib/sikuli to Jython's Lib/site-packages)
for this you have to unjar sikuli-script.jar, to get to the mentioned folder
since site-packages is in the standard sys.path, this should work:
from sikuli import *
popup("hallo")

3. option (add ... path-to-your... /sikuli-script.jar/Lib to the Netbeans property file)
Since it is still not possible to do this in the properties dialog (not possible to step inside a jar), you can do this trick:
- add ... path-to-your... /sikuli-script.jar TO THE END of the Jython's config PythonPath
- close Netbeans
- edit Netbeans config file and add /Lib to the existing line
- restart Netbeans and check the properties

again this should work then:
from sikuli import *
popup("hallo")

BE AWARE: in all cases you need the ref to Sikuli's libs folder in the system path at runtime.

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

LOL, just realized, that I already outlined the solution steps here:
https://github.com/RaiMan/SikuliX-API/wiki/Usage-in-Java-programming

Revision history for this message
tlondon (telconstar99) said :
#5

Thanks for all the responses. Unfortunately I ran into a bit of a problem following your tutorial RaiMan.

"take care, that sikuli-script.jar is in the Java classpath of your project"

How do I add a .jar to a Python project? I'm using Netbeans. Thanks so much.

Revision history for this message
tlondon (telconstar99) said :
#6

Nevermind, it was an error between the user and the keyboard. Oh, and I had a typo above, meant Java project.

Revision history for this message
tlondon (telconstar99) said :
#7

Thanks RaiMan, that solved my question.