【问题标题】:how to pass arguments to python script in java using jython如何使用jython将参数传递给java中的python脚本
【发布时间】:2016-07-17 03:32:18
【问题描述】:

我正在尝试使用 jython 在 java 中执行我的 python 脚本。 重要的是我需要使用 jython 将命令行参数传递给我的脚本,例如myscript.py arg1 arg2 arg3。 这里有一个类似的问题:Passing arguments to Python script in Java

没有完全回答(所有解决方案都不起作用)。

我的代码现在看起来像这样:

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

但是,这似乎没有将任何参数传递给 python 脚本。 任何建议如何正确地做到这一点? 一定很简单,但我在网上找不到任何文档。

我正在使用 python 2.7 和 jython 2.7.0。

【问题讨论】:

    标签: java python command-line-arguments jython


    【解决方案1】:

    这里有一个小代码:

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

    这会在 /path/to/script 目录中调用一个名为 myscript.py 的 python 脚本。

    myscript.py 的一个例子:

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

    【讨论】:

    • 它表示此变量的“void 是无效类型”。我是java新手,应该是其他类型吗?
    • 等等……你想调用的函数的名称是什么?我不认为你想直接调用脚本...否则没有必要使用 Jython...只需使用系统调用
    • 我想直接调用脚本。我必须使用 jython(这不是我的选择......)。主要原因是,如果你使用 jython,那么你的本地机器上是否安装了 python 并不重要。
    • 仍然收到此错误:线程“main”中的异常 java.lang.Error:未解决的编译问题:void 是变量 myFunctionJavaWrapper 的无效类型令牌“(”上的语法错误,预期语法错误标记 ",", ; 标记 ",", 的预期语法错误;预期语法错误,插入 ";" 以完成 LocalVariableDeclarationStatement
    【解决方案2】:

    与此同时,我找到了解决方案。这里是:

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

    我需要做的只是将我的脚本作为 arg[0] 添加到传递的参数中(参见代码的第一行)!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 2015-05-13
      • 2018-08-14
      • 1970-01-01
      • 2010-10-26
      相关资源
      最近更新 更多