Script Sikuli in Eclipse - pythonpath problem

Asked by j

Hi all,

I followed the tutorial and integrated sikuli in eclipse using pyDev.
Everything works fine until I start importing my custom sikuli modules.
When I have a script like:

module1.sikuli
# code inside module1 here

module2.sikuli
import module1

Eclipse can't find module1.py, I guess because the module1.sikuli is not exlpicitly added to the sourcepath list. In sikuli-ide, sikuli imports other .sikuli modules just like .py modules, but this is not working in eclipse. Since I have many and varying sikuli modules, I do not want to manage all these sourcepath-imports manually in eclipse.

So is there any way to
1) use the sikuli-ide "import sikuli modules like py modules" feature in eclipse or
2) manage the python path automatically with eclipse?

Any hints/help would be very nice.

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
RaiMan (raimund-hocke) said :
#1

When using Sikuli scripts in PyDev eclipse, the standard import feature for .sikuli does not work automatically, since the sys.path settings are special inside Eclipse PyDev.

So you have to either add the path, where your import modules live, to your project settings or you have to adjust sys.path in your script.

After that, the import feature can be used the same way as in the IDE.

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

To be clear:
You only have to add the folder, that contains the .sikuli scripts, that you want to import.

Revision history for this message
j (j-the-k) said :
#3

Maybe I'm missing something, at the moment I have two .sikuli modules in the same directory (mydir = path to modules):
mydir/module1.sikuli
# code inside module1 here

mydir/module2.sikuli
import module1

This runs with sikuli-ide.sh -r module2.sikuli
but when I run it in Eclipse I get
Traceback (most recent call last):
  File "mydir/module2.sikuli/module2.py", line 1, in <module>
    import module1
ImportError: No module named module1

It does not help if I append "mydir" to sys.path, however it works if I append "mydir/module1.sikuli/" to sys.path.
But this is not needed when running the script with sikuli-ide.bat so I would not want to do that if not neccessary.

"You only have to add the folder, that contains the .sikuli scripts, that you want to import."
Am I doing something wrong or why is this not working in my eclipse?

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

Only to be clear:
A module2.sikuli cannot be run in Eclipse PyDev. It is the contained module2.py. that is edited and run. And in order to use any Sikuli features, it needs an "from sikuli import *" at the beginning.
And you are left alone with the image path, if your module2.sikuli contains images.

It works for me in Eclipse Indigo PyDev:

- a workspace with 2 .sikuli
test.sikuli
sub.sikuli
no src folder, workspace folder in sys.path per project settings

# test.sikuli
from sikuli import *
import sub

# sub.sikuli
from sikuli import *
print "hello from sub"

Revision history for this message
j (j-the-k) said :
#5

Okay, I seem to have imported the Lib/sikuli directory incorrect.
Thanks for your help, the imports are found now. =)
However, there are two other problems I encountered:

1. getBundlePath() always returns None when I run my scripts in Eclipse. It doesn't when run in sikuli-ide. This is not so important for me because I only used getBundlePath parallel to sys.argv[0] so I can replace it with the latter.

2. I have a Sikuli module named logging.sikuli. This is imported correctly in sikuli-ide. In Eclipse, the Sikuli importer does NOT import my logging.sikuli but the Jython logging module (Jython2.5.2/Lib/logging). I debugged this problem and found the source in SikuliImporter.py in method def _load_module(self, fullname): the imp module fetches the wrong module (jython logging) although the Sikuliimporter has the right path to the sikuli module. I solved this by renaming my module, but this is some kind of bug.

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

-- 1.
getBundlePath() only works, when the script is run using sikuli-script.jar (internally used by the IDE).
So you might configure your Eclipse project, so that sikuli-script.jar is used as Interpreter.
On the other hand there are many options to configure the runtime environment in Eclipse.

--2.
In the standard Eclipse configuration, the Jython standard libs are positioned in sys.path before the other paths that are added afterwards by a running script (which is e.g. done by Sikuli's import support).
This cannot be solved by the script user when running in Eclipse.
So it was the right solution to rename your stuff.
This is not really a bug, since it is "by design". But you might post a request bug, that Sikuli should add dynamic sys.path elements at the top.

Revision history for this message
j (j-the-k) said :
#7

Thanks for your reply.
There is one more thing I hope you can help me with. I really appreciate your patient answers and hope you can help me one last time (hopefully).

I now have my project imported in eclipse, Lib/sikuli is in jython-intallation/Lib/site-packages and appears in the eclipse package manager jython interpreter in System Libs -> Lib/site-packages as intended.
sikuli-script.jar is added in project properties -> PYTHONPATH -> External Libraries.

All scripts work right but code completion does not work with the Sikuli elements.

"from sikuli.Sikuli import *" imports Lib/site-packages/sikuli/Sikuli.py but Eclipse always marks it as "unused wild import".
Sikuli elements like click(), SCREEN, wait() and so on are marked as e.g. "Undefined variable: hover".
Inside "imports Lib/site-packages/sikuli/Sikuli.py", Eclipse does not display errors but I cannot use code completion, for the Sikuli Java packages. If I type org. and press CTRL+Space, some possibilites are shown, but not sikuli.

Is this a known problem or did I do something wrong again?
Thanks for your patience with me =)

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

--- Sikuli elements like click(), SCREEN, wait() and so on are marked as e.g. "Undefined variable: hover".
look https://answers.launchpad.net/sikuli/+question/196888 comment #3

Since most of the methods are defined on the Java level, the PyDev code completion cannot be used.

Revision history for this message
j (j-the-k) said :
#9

Thanks RaiMan, that solved my question.

Revision history for this message
Reinaldo Burgos (reinaldo-burgos-y) said :
#10

Hi,

I wan to know If sikuli 1.0.1 support to create script on eclipse using python/jython?
I have maked following step.
Step 1: I have done a sikuli installation on windows xp, and choosed the alt 3.
Step 2: I have added python pluggin on eclipse
Step 3: I put the sikuli.jar under pidev-pythonpath external lib in eclipse
Step 4 I try to create a project using sikulis lib.

The problem is that the sikulis method are not recognized in the py file I created under python project.

I will appreciate if you could give some support to resolve the impediment.

Revision history for this message
RaiMan (raimund-hocke) said :
#11
Revision history for this message
Reinaldo Burgos (reinaldo-burgos-y) said :
#12

HI,
Thank you for a quick answer.

Next question: It's possible please that you provide a document that describing the steps? like this one http://code.nicholasnewell.com/2012/10/04/setting-up-eclipse-for-sikuli-development/

I mean the are very popular today using sikuli as Automationtest and put together sikuli script with a nice final test raport are desired. Next step are to debug by solution and a good developer emviroment is Eclipse, I think! if you get what i I mean

//RB

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

Thanks for the link.

The there mentioned steps are still valid.

we have the basic steps here:
http://doc.sikuli.org/faq/040-other-ide.html
as mentioned in the blog post.

additional conveniences you might use are behind the link from comment #11

I will work on the update of documentation later this year.

Revision history for this message
Carlos (cillana) said :
#14

Hi,

I have a similar problem since I am able to run sikulii in eclipse as the src/ were all *.sikuli scripts are is in the sysPath but it is not doing code completion.
If I add the *.sikuli to the PYTHONPATH I am able to have code completion but when running the script Sikuli is not able to find the images.
I may organize the scripts as described in https://answers.launchpad.net/sikuli/+question/240262 with all .py sikuli scripts in a directory and the images in another which is a directory structure I prefer rather than the .sikuli directories.
But when doing that, sikuliIDE can not load the .py files as it is looking for .sikuli directories and I like how fast I can capture images with sikuliIDE (and set offsets to images).

Is there any way so that I can load .py directly in sikuliIDE??

Thanks,
Carlos