Python Interpreter In Jython
All I'm trying to do is pass an argument to the python interpreter so it can be passed as an argument for a module. E.g. I have the following defined in a py file: def print_tw
Solution 1:
This should work:
interp.exec("import YOUR_PYTHON_FILE.py");
interp.exec("YOUR_PYTHON_FILE.print_twice('Adam')");
Its equivalent in a python console is this:
>>>import YOUR_PYTHON_FILE.py>>>YOUR_PYTHON_FILE.print_twice('Adam')
Adam
Adam
Solution 2:
You shouldn't need to explicitly compile the script, just import it and the interpreter will take care of compilation. Something like this (assuming printTwice.py
is in the working directory of your program:
interp.exec("from printTwice import print_twice");
interp.exec("print_twice('Adam')");
You don't need to use interp.eval
on the second line assuming that print_twice
does actually contain print
statements; if it just returns a string then you probably want
System.out.println(interp.eval("print_twice('Adam')"));
.
Post a Comment for "Python Interpreter In Jython"