[英]how to pass arguments to python script in java using jython
I am trying to execute my python script in java using jython. 我正在尝试使用jython在Java中执行我的python脚本。 The important thing is that I need to pass command line arguments to my script using jython, eg myscript.py arg1 arg2 arg3.
重要的是,我需要使用jython将命令行参数传递给脚本,例如myscript.py arg1 arg2 arg3。 There is a similar question here: Passing arguments to Python script in Java
这里有一个类似的问题:将参数传递给Java中的Python脚本
Which was not answered completely (none of the solutions work). 尚未完全回答(解决方案均无效)。
My code now looks like this: 我的代码现在看起来像这样:
String[] arguments = {"arg1", "arg2", "arg3"};
PythonInterpreter.initialize(props, System.getProperties(), arguments);
org.python.util.PythonInterpreter python = new org.python.util.PythonInterpreter();
StringWriter out = new StringWriter();
python.setOut(out);
python.execfile("myscript.py");
String outputStr = out.toString();
System.out.println(outputStr);
However, this does not seem to pass any arguments to the python script. 但是,这似乎没有将任何参数传递给python脚本。 Any suggestions how to do it properly?
有什么建议如何正确执行吗? It must be simple, but I can't find any documentation on the web.
它必须很简单,但是我在网络上找不到任何文档。
I am using python 2.7 and jython 2.7.0. 我正在使用python 2.7和jython 2.7.0。
In the meantime, I have found a solution. 同时,我找到了解决方案。 Here it is:
这里是:
String[] arguments = {"myscript.py", "arg1", "arg2", "arg3"};
PythonInterpreter.initialize(System.getProperties(), System.getProperties(), arguments);
org.python.util.PythonInterpreter python = new org.python.util.PythonInterpreter();
StringWriter out = new StringWriter();
python.setOut(out);
python.execfile("myscript.py");
String outputStr = out.toString();
System.out.println(outputStr);
What I needed to do was simply to add my script to the passed parameters as arg[0] (see the first line of the code)! 我要做的只是将我的脚本作为arg [0]添加到传递的参数中(请参见代码的第一行)!
Here a small code to do so: 这里是一个小代码:
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.__builtin__;
import org.python.util.PythonInterpreter;
public class JythonTest {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
String fileUrlPath = "/path/to/script";
String scriptName = "myscript";
interpreter.exec("import sys\n" + "import os \n" + "sys.path.append('" + fileUrlPath + "')\n"+ "from "+scriptName+" import * \n");
String funcName = "myFunction";
PyObject someFunc = interpreter.get(funcName);
if (someFunc == null) {
throw new Exception("Could not find Python function: " + funcName);
}
try {
someFunc.__call__(new PyString(arg1), new PyString(arg2), new PyString(arg3));
} catch (PyException e) {
e.printStackTrace();
}
}
}
this call a python script in directory /path/to/script which is called myscript.py. 这会在目录/ path / to / script中调用python脚本,称为myscript.py。
an example of myscript.py: myscript.py的示例:
def myscript(arg1,arg2,arg3):
print "calling python function with paramters:"
print arg1
print arg2
print arg3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.