debugging Sikuli in Pycharm

Asked by rod

Hi all,

I'm using Sikuli 1.0.1 on Windows7 with Java7 and Pycharm as IDE for debugging.
For that I :
- installed jython2.5.3 and used it as Pycharm Jython interpreter
- extracted from sikuli-ide.jar the folder Lib\sikuli and added it to the sys.path.
- added the C:/apps/SikuliX/sikuli-ide.jar to the CLASSPATH env variable

Thanks to that, I could run successfully some Sikuli scripts like this :

from sikuli import *
result = find("myImage.png")
result.highlight(3)

Now, I wanted to use the Pycharm debug but, for the same script, I got the following error :

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevd.py", line 1733, in <module>
    debugger.run(setup['file'], None, None)
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pydev\pydevd.py", line 1226, in run
    pydev_imports.execfile(file, globals, locals) # execute the script
  File "C:/Users/fabio_iervolino/PycharmProjects/test2/test.py", line 19, in <module>
    from sikuli import *
  File "C:\jython2.5.3\Lib\site-packages\sikuli-ide\Lib\sikuli\__init__.py", line 7, in <module>
    from Sikuli import *
  File "C:\jython2.5.3\Lib\site-packages\sikuli-ide\Lib\sikuli\Sikuli.py", line 10, in <module>
    import __main__
ImportError: No module named __main__

No Idea of what this module __main__ is ....
The problem raises when I execute the "from sikuli import *" in debugging => I guess it is not a generic debug problem but something to do with the Sikuli import.

Could someone help me please ?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
rod
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

No idea, why this
import __main__
does not work in the debugger environment.

but you might comment out this line in Lib/sikuli/Sikuli.py (line 10) and see what happens.

BTW: this is from the previous versions I have taken over. I never checked what that helps or wether it makes sense at all.

Anyway: thanks for finding and pointing out.

… and above all: thanks for the hint on PyCharm, which seems to be a nice IDE besides Eclipse/PyDev.

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

I guess in the debugging situation, the script to debug is not accessed in the way, that the name __main__ is defined (it is the "name" of the top level script, that is loaded and run in the interpreter)

the script to debug is obviously run using
pydev_imports.execfile(file, globals, locals) # execute the script

which definitely does not setup a __main__ module context.

I think you might safely comment out the statement
import __main__
in
Lib/sikuli/Sikuli.py (line 10)

I already did that in the current 1.1.0-Beta4 and will have a look, if this has any negative consequences.

Revision history for this message
rod (rodhiguain) said :
#3

Thanks a lot RaiMan, I comment out the line Lib/sikuli/Sikuli.py (line 10) and It worked !!!

For the moment I did not find any side effect ...

Just a last question.
Before using Pycharm I was launching sikuli in the script mode (-r).
Something like :
"%SIKULI%"\runIDE.cmd -r myMain.sikuli --args -P myargP

Now, I'd like to launch the same command and put some break points to debug my scripts .sikuli called by myMain.sikuli file ...
What's the best way to do that ?

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

I just played around a little bit with PyCharm and 1.1.0-Beta4.

looks nice and promising.

some recommendations:
- according to the new structures and usages with 1.0.1+, you should not use sikuli-ide.jar/sikuli-script.jar (1.0.1) nor sikulix.jar (1.1.0+), since these jars contain a compete Jython, that might get in conflict with your local python.
instead use sikuli-java.jar (1.0.1) or sikulixapi.jar (1.1.0+)
According to the setup recommendations the jar should be in a user writable folder like c:\SikuliX

- I do not recommend the global system environment variable CLASSPATH, but instead in the project configuration as additional Environment variable.

I will add more convenience support to version 1.1.0 for the usage of Eclipse/Pydev and PyCharm.

--- your question about debugging:
to debug a script, just load it into PyCharm, set at least one breakpoint (click left in the vertical bar left of the source) and trigger Debug run (shift-F9 or entry in Menu Run)
to add "command line" args use the project configuration (script parameters)

Revision history for this message
rod (rodhiguain) said :
#5

Thanks RaiMan,

I used the sikuli-java.jar (instead of sikuli-ide.jar) and defined a project env variable CLASSPATH (instead of a global system one), but I'm still stuck on how to launch sikuli files in script mode.

Let me please explain my issue once again.

Currently I have a main sikuli file (called myMain.sikuli) that do not contain any reference (no from sikuli import * ...) to sikuli.
It just import unittest and manage some unittest suites.
These suites are actually some .sikuli files that import sikuli (from sikuli import *) and are located somewhere.

So, for example, if I want to execute the suite mySuite1.sikuli I just need to call (by a Windows console) the following command :

"%SIKULI%"\runIDE.cmd -r myMain.sikuli --args -S mySuite1

And It works fine.

Now, I would like to run the same command directly in pycharm (and in debugging would be better) but I do not know how to do it.
Actually in the runIDE.cmd several variables (SIKULIX_HOME, PARMS, etc. etc.) are set and I cannot set them in pycharm if I execute only (in the pycharm Run Configuration) :
myMain.sikuli --args -S mySuite1

How do I can load and execute the runIDE.cmd in Pycharm before running my myMain.sikuli and call my sikuli suites ?

Thanks again for your help,
Rod

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

Big misunderstanding:
In IDE's like PyCharm, you cannot use the runIDE.cmd (because this uses Java to run the sikuli-ide.jar, that in turn contains a Jython interpreter). You simply open some project and run it according to the project settings.

In your case, you use Jython directly as script runner.

So you simply have to open any of your scripts in PyCharm, take care about the needed settings in the project configuration (including parameters for your script) and issue the run command (button, menu or shortcut).

Revision history for this message
rod (rodhiguain) said :
#7

Thanks a lot.
all work fine now !!!!