Jython中的Python解释器 [英] Python Interpreter in Jython
本文介绍了Jython中的Python解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要做的就是将参数传递给python解释器,以便可以将其作为模块的参数传递.
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.
例如我在py文件中定义了以下内容:
def print_twice(test):
print test
print test
我想将参数"Adam"传递给它,所以我尝试了:
// Create an instance of the PythonInterpreter
PythonInterpreter interp = new PythonInterpreter();
// The exec() method executes strings of code
interp.exec("import sys");
interp.exec("print sys");
PyCode pyTest = interp.compile("Adam", "C:/Users/Adam/workspace/JythonTest/printTwice.py");
System.out.println(pyTest.toString());
我也尝试过:
interp.eval("print_twice('Adam')");
我一直在使用以下Jython API,但我不太了解: http://www.jython.org/javadoc/org/python/util/PythonInterpreter.html#compile%28java.lang.String,%20java.lang.String%29
I've been using the following Jython API but I don't understand it well: http://www.jython.org/javadoc/org/python/util/PythonInterpreter.html#compile%28java.lang.String,%20java.lang.String%29
非常感谢您的建议.
谢谢
推荐答案
这应该有效:
interp.exec("import YOUR_PYTHON_FILE.py");
interp.exec("YOUR_PYTHON_FILE.print_twice('Adam')");
在python控制台中,它的等效项是:
Its equivalent in a python console is this:
>>> import YOUR_PYTHON_FILE.py
>>> YOUR_PYTHON_FILE.print_twice('Adam')
Adam
Adam
这篇关于Jython中的Python解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文