Jython调用不包含第三方库的python脚本
2017-11-15 08:51
1406 查看
1、本地环境安装的是Python 2.7.11
2、用maven下载jython依赖
<pre name="code" class="html"><dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7.0</version>
</dependency>
3、python脚本编写
#coding:utf-8
def adder(a, b):
return a + b
def mytest(str2):
print str2
return 'call success !!!'
4、Java调用Python
<pre name="code" class="java">package test1;
import java.util.Properties;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
public class Java2Python {
public static void main(String args[]) {
Properties props = new Properties();
props.put("python.home","D:/Python27/Lib");
props.put("python.console.encoding", "UTF-8"); // Used to prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
props.put("python.security.respectJavaAccessibility", "false"); //don't respect java accessibility, so that we can access protected members on subclasses
props.put("python.import.site","false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("E:/workspace3/test1/src/main/java/test1/my_utils.py");
PyFunction adder = (PyFunction) interpreter.get("adder", PyFunction.class);
int a = 30, b = 50;
PyObject pyobj = adder.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("anwser = " + pyobj.toString());
PyFunction mytest = (PyFunction) interpreter.get("mytest", PyFunction.class);
PyObject pyobj2 = mytest.__call__(new PyString("this is java project!!!"));
System.out.println(pyobj2.toString());
interpreter.close();
}
}
以上方式可以实现Java调用Python,但是在python 脚本中只能有python的原生api,如果在在脚本中有引入pandas,numpy之类的第三方扩展包,还是会是会找不到,这个问题正在查找是什么原因..
2、用maven下载jython依赖
<pre name="code" class="html"><dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7.0</version>
</dependency>
3、python脚本编写
#coding:utf-8
def adder(a, b):
return a + b
def mytest(str2):
print str2
return 'call success !!!'
4、Java调用Python
<pre name="code" class="java">package test1;
import java.util.Properties;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
public class Java2Python {
public static void main(String args[]) {
Properties props = new Properties();
props.put("python.home","D:/Python27/Lib");
props.put("python.console.encoding", "UTF-8"); // Used to prevent: console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.
props.put("python.security.respectJavaAccessibility", "false"); //don't respect java accessibility, so that we can access protected members on subclasses
props.put("python.import.site","false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("E:/workspace3/test1/src/main/java/test1/my_utils.py");
PyFunction adder = (PyFunction) interpreter.get("adder", PyFunction.class);
int a = 30, b = 50;
PyObject pyobj = adder.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("anwser = " + pyobj.toString());
PyFunction mytest = (PyFunction) interpreter.get("mytest", PyFunction.class);
PyObject pyobj2 = mytest.__call__(new PyString("this is java project!!!"));
System.out.println(pyobj2.toString());
interpreter.close();
}
}
以上方式可以实现Java调用Python,但是在python 脚本中只能有python的原生api,如果在在脚本中有引入pandas,numpy之类的第三方扩展包,还是会是会找不到,这个问题正在查找是什么原因..
numpy , scipy 都是 c python 的第三方模块,是用 c (部分 c++, 和 fortran )写的,必然不支持
相关文章推荐
- C#调用Python脚本并使用Python的第三方模块
- C#调用Python脚本并使用Python的第三方模块
- Windows C++程序调用包含TensorFlow代码的Python脚本出错问题的解决办法
- Jython:java调用python文件之第三方包路径问题
- Jython:java调用python文件之第三方包路径问题
- 在C#中调用python脚本,并使用python第三方arcpy模块
- C#调用Python脚本并使用Python的第三方模块
- C#调用Python脚本并使用Python的第三方模块
- java调用python总出现python第三方库没有定义
- python调用shell脚本
- C调用Python脚本文件里的函数
- python调用linux shell脚本,并返回结果一例
- python调用bat脚本的子线程处理
- 如何调用已经写好的Python脚本
- python调用shell脚本的方法
- PHP文件中调用Python脚本
- python实现调用其他python脚本的方法
- python2.7嵌入c++的故事之一:c++调用脚本出现Import error: no module named错误
- DIY简易Python脚本调用AWVS扫描
- Java调用Python脚本工具类