好的,所以here是已经提出的问题,但是答案似乎并没有太大帮助。我能够使用jython运行python脚本并在该问题中发布答案,但是我无法传递变量...当我运行程序时,错误提示没有arg1 arg2和arg3这样的变量...我是什么?做错了吗?

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);


这是python脚本

def myFunction(arg1,arg2,arg3):
    print "calling python function with paramters:"
    print arg1
    print arg2
    print arg3
myFunction(arg1,arg2,arg3)


现在错误说未声明arg1 arg2和arg3

最佳答案

您应该通过sys.argv变量访问命令行参数,如下所述:https://www.tutorialspoint.com/python/python_command_line_arguments.htm

所以正确的代码:

myFunction(sys.argv[0], sys.argv[1], sys.argv[2])

10-04 23:38
查看更多