json import error

Asked by Michaela

I want to import a module (written in python2.4) which is using json and simplejson into my own sikuli program.
I included therefore json_simple-1.1.jar and jyson-1.0.2.jar (from opensource.xhaus.com/projects/show/jyson) in the class path of sikuli.ini. Both jar files are in the sikuli installation folder. I also included "from sikuli import *" in the module I want to import at the very beginning. The module is in the same folder as my own written module.

When running Sikuli-IDE, I get the error message in that import file:

"File "modulexxx.py (=name of the import module)", line 94, in
from json import loads as json_decode, dumps as json_encode
ImportError: No module named json"

The code excerpt from the module I want to import:
-----
try:
    from json import read as json_decode, write as json_encode
except ImportError:
    try:
        from json import loads as json_decode, dumps as json_encode
    except ImportError:
        from simplejson import loads as json_decode, dumps as json_encode <- this is line 94
----

Does anyone now how I get the import of the module work?
I am a sikuli and python beginner, so any help would be highly appreciated!

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

nice try ;-)

Since you are working on the Python level (Sikuli IDE uses the Java based Jython interpreter), you have to use Python modules in the first place, if you are using modules, that expect Python code or API's.

What you downloaded are Java classes, that might principally be used in Sikuli scripts too, but only, if the API names and the signatures are fully compatible. But usually, the imports are different, since java packages are dot-structured usually - in this case: org.json.simple. So this only might have a chance to work, if you are coding the imports yourself (not the fact in this case).

So in your case, the easiest trial might be, to use the simple-json implementation from:
http://pypi.python.org/pypi/simplejson/
Download and uncompress somewhere, but do not install it!
Copy the contained folder simplejson to the folder Lib in the Sikuli X program folder (you might have to create it). This folder is in the sys.path already, so anything stored here, can be imported without any sys.path manipulations.

Come back for more help.

Revision history for this message
Michaela (mifischer) said :
#2

Thanks RaiMan, that solved my question.

Revision history for this message
Michaela (mifischer) said :
#3

awesome :) Thanks for your fast help!!

Revision history for this message
Roman Podolyan (podolyan-roman) said :
#4

Let's back to this question from 1.0.1 perspective.
For me adding simplejson to \libs folder doesn't seem to work now.
Am I doing something wrong (and how to check what?) or now simplejson import should be done other way?

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

This is still valid (and will ever be ;-)

When running Sikuli using either sikuli-ide or sikuli-script, sys.path always contains these 2 entries:

path-to-folder-containing-sikuli-xxx.jar/Lib
path-to-folder-containing-sikuli-xxx.jar/sikuli-xxx.jar/Lib

The second one is inside the jar, so usually not reachable.
But the first one is (you might have to create it first).

This was the suggestion in comment #1 (... and not the libs folder ;-)