Jython: preload PythonInterpreter with values for use in scripts

H

Harald Kirsch

How can I set variables in PythonInterpreter such that
scripts then run via import can have access to it.

My idea was to go along the lines of:

PythonInterpreter ip = new PythonInterpreter();
// make the object about to use the interpreter
// available to that interpreter
ip.set("javaCaller", this);
ip.exec("import mystuff");

Within mystuff.py I wanted to do things like

m = sys.modules['__main__']
m.javaCaller.setThoseNiftyFeaturesOfTheCallingJavaObject("hello")

Alas, sys.modules does not contain '__main__'.

Is there any way to get hold of locals() of PythonInterpreter ip within
a module imported as shown above?

I would expect that execfile allows the above communication,
but then I have to do all the searching for the module myself.
Therefore a solution where the module is simply imported as above
is preferred.

Thanks,
Harald.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top