Using external python module using json

Asked by MicMarcil

Hello,

I want to use an external python module that uses json internally.
I took a look at this post "https://answers.launchpad.net/sikuli/+question/193318" which is not far from my problem.

I understand that json isn't part of Jython, and that I can workaround this using simplejson.

I downloaded and integrated simplejson in the Lib folder, inside my Sikuli installation.
I realised after that simplejson seems to be already part of Sikuli 1.0.1 and that Jython version :)

So, back to my json problem... when I import my external module and uses the function from this module, I always get :
[error] NameError ( global name 'json' is not defined )

I tried things like : import simplejson as json
But I can't find a way to make my script work. I always come back to this error :(

Here's an example of a non-working code, written directly in my Sikuli script:
    client = APIClient('some_server')
    client.user = 'some_user'
    client.password = 'some_password'

    result = client.send_post('some_json') <== [error] NameError ( global name 'json' is not defined )

The module is using a APIClient class internally... is this conflicting with an APIClient class already existing in Jython? Is is the same class?

I'm afraid I might have to adapt this external module... (I'll do it if I have no choice)
I'm a bit loss trying to get this external module to work in Sikuli :(

Thanks for the help.
Michael

Question information

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

To clarify the situation do the following on command line:

java -cp absolute-path-to-sikuli-ide.jar(or -script.jar) org.python.util.jython

this should open an interactive Jython session, that does not know anything about SikuliX yet.

now do your APIC stuff and see what happens.

to activate SikuliX at any time later:
import org.sikuli.basics.SikuliXforJython # appends sikuli-xxx.jar/Lib to sys.path
from sikuli import *

Revision history for this message
MicMarcil (micmarcil) said :
#2

Thanks for the quick answer RaiMan.

I tried several things in the interactive Jython to find out the problem. That was very useful.

I ended up doing as follow :

1. Downloaded the simplejson module. Put it in the Lib folder I created, in the Sikuli folder.

2. In my external module, I replaced json import by the following:
import simplejson as json

3. Can now use json in Sikuli :
from myExternalModule import * # simplejson import done in my external module
do my stuff

Thanks again Raiman
Michael