您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Jython:java调用python文件之第三方包路径问题

5b51 2022/1/14 8:25:23 python 字数 9159 阅读 825 来源 www.jb51.cc/python

本方法解决python代码的可移植性,不需要在新机器上配置python环境,只通过安装jython的方式将python代码嵌入java工程

概述

方法解决python代码的可移植性,不需要在新机器上配置python环境,只通过安装jython的方式将python代码嵌入java工程

@H_419_3@1. Jython如何安装

下载地址:nofollow">jython_installer-2.5.0.jar  。下载需要积分,如果无积分,可联系楼主。

傻瓜式下一步安装方式,路径最好和工程选在同一目录下。

@H_419_3@2. 运行python代码

举个简单例子,安装好java环境及eclipse之后,copy如下代码即可运行。注释部分为运行文件的方式,需要新建文件后去掉注释执行。

文件
        //  interpreter.exec("filepy = \"E:\\test.py\"");  
        //  interpreter.execfile(filepy);  ///执行python py文件  
        //  filepy.close(); 
    } 
}  

文件运行方式中,

第一种情况:如果.py文件中包含同一目录的自定义包,则在eclipse工程下可正常调用

第二种情况:如果包含第三方包,需要拷贝到当前目录下。

第三种情况:由于路径问题无法调用。Jython/lib文件下有大量第三方包,如os,re,__future__,重复拷贝费时费力,这时可419_3@手动修改path路径。(推荐)

修改路径有两种方式:

@H_419_3@(1) Java方式

stemState;
System.out.println(sys.path.toString());    // prevIoUs
PySystemState sys = Py.getSystemState();   
sys.path.add("E:\\sacaapm-paserver\\src-python\\jython\\Lib"); 
System.out.println(sys.path.toString());   // later

代码嵌入part 2代码,得到完整代码后执行,可以看到前后路径发生变化。

@H_419_3@(2) python方式

@H_419_3@(3) 灵活运用以上两种方式或其组合形式。

@H_419_3@错误,建议使用第一种。

完整代码如下:

import org.python.core.Py;
import org.python.core.PySystemState;
import org.python.util.PythonInterpreter;

public class test {
public static void main(String args[]) throws IOException {
PythonInterpreter interpreter = new PythonInterpreter();
PySystemState sys = Py.getSystemState();
sys.path.add("E:\src-python\jython\Lib");
interpreter.exec("import sys");
interpreter.exec("print sys.path");
interpreter.exec("path = \"E:\src-python\jython\Lib\"");
interpreter.exec("sys.path.append(path)");
interpreter.exec("print sys.path");
interpreter.exec("a=3; b=5;");
InputStream filepy = new FileInputStream("E:\input.py");
interpreter.execfile(filepy);
filepy.close();
}
}

public class test {
public static void main(String args[]) throws IOException {
PythonInterpreter interpreter = new PythonInterpreter();
PySystemState sys = Py.getSystemState();
sys.path.add("E:\src-python\jython\Lib");
interpreter.exec("import sys");
interpreter.exec("print sys.path");
interpreter.exec("path = \"E:\src-python\jython\Lib\"");
interpreter.exec("sys.path.append(path)");
interpreter.exec("print sys.path");
interpreter.exec("a=3; b=5;");
InputStream filepy = new FileInputStream("E:\input.py");
interpreter.execfile(filepy);
filepy.close();
}
}

public class test {
public static void main(String args[]) throws IOException {
PythonInterpreter interpreter = new PythonInterpreter();
PySystemState sys = Py.getSystemState();
sys.path.add("E:\src-python\jython\Lib");
interpreter.exec("import sys");
interpreter.exec("print sys.path");
interpreter.exec("path = \"E:\src-python\jython\Lib\"");
interpreter.exec("sys.path.append(path)");
interpreter.exec("print sys.path");
interpreter.exec("a=3; b=5;");
InputStream filepy = new FileInputStream("E:\input.py");
interpreter.execfile(filepy);
filepy.close();
}
}

@H_419_3@E:\\input.py文件代码

其中变量a,b的值由java传入。

总结

以上是编程之家为你收集整理的Jython:java调用python文件之第三方包路径问题全部内容,希望文章能够帮你解决Jython:java调用python文件之第三方包路径问题所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶