1、導(dǎo)入Jar
找到j(luò)ython-installer-2.7.0.jar 包里面的jpython.jar蚣常,加載到工作目錄
我用的java開發(fā)工具是IDEA市咽、JDK8、Python3.5
2 抵蚊、執(zhí)行python源碼
獲取一個元組里面的元素
import org.python.util.PythonInterpreter;
public class FirstJavaScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days[1];");
}// main
}
一般會出現(xiàn)以下異常
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
* sys.path: ['C:\\Users\\Administrator\\IdeaProjects\\MyText\\lib\\Lib', '__classpath__', '__pyclasspath__/']
This attribute might be including the wrong directories, such as from CPython
* sys.prefix: C:\Users\Administrator\IdeaProjects\MyText\lib
This attribute is set by the system property python.home, although it can
be often automatically determined by the location of the Jython jar file
You can use the -S option or python.import.site=false to not import the site module
看錯誤可以理解為一些庫的路徑錯誤施绎,下面也給出了解決方法
我們只需要吧代碼改為以下這個例子就可以順利執(zhí)行了。
Properties props = new Properties();
props.put("python.home", "path to the Lib folder");
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days[1];");
執(zhí)行結(jié)果是Tue
3贞绳、調(diào)用.py中的方法
test.py的源碼
def add(a, b):
return a + b
Java源碼
Properties props = new Properties();
props.put("python.home", "path to the Lib folder");
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("test.py");
PyFunction func = (PyFunction) interpreter.get("adder",
PyFunction.class);
int a = 100, b = 100;
PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("anwser = " + pyobj.toString());
執(zhí)行結(jié)果為 200
4谷醉、執(zhí)行 .py文件
test.py
# -*- coding: utf-8 -*
print ("hello")
ls = [1,2,3,4,5,6]
print(ls)
print('你好')
java代碼
Properties props = new Properties();
props.put("python.home", "path to the Lib folder");
props.put("python.console.encoding", "UTF-8");
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("test.py");
執(zhí)行結(jié)果:
hello
[1, 2, 3, 4, 5, 6]
你好
如果報出如下異常,請在源碼加上
# -- coding: utf-8 -
Exception in thread "main" SyntaxError: Non-ASCII character in file