java调用python传参返回计算结果

evezero2 evezero2 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
项目想用java调用python脚本进行传参计算并返回结果。使用Jython.jar的PythonInterpreter类。
PythonInterpreter interpreter = new PythonInterpreter();

PySystemState sys = Py.getSystemState();
sys.path.add("pythonLib/Lib");
// interpreter.exec("import MAXREPEAT");

interpreter.exec("import json");
interpreter.execfile(is); // .py脚本
PyFunction func = (PyFunction) interpreter.get(funcName,
PyFunction.class);// 脚本中函数名
PyObject pyobj = func.__call__(new PyString(jsonString)); // 函数返回值

遇到问题,需要把每一个模块路径添加进去,但是python脚本import json时报错了。jython跟python版本之间有啥要求吗,凌乱了

Python2.7.11版本
Jython2.5.3(没有json模块)与jython2.7.0(lib下有json模块)遇到问题不一样
2.5.3
模块里的模块报错
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "pythonLib\Lib\json\__init__.py", line 108, in <module>
from .decoder import JSONDecoder
File "pythonLib\Lib\json\decoder.py", line 3, in <module>
import re
File "pythonLib\Lib\re.py", line 105, in <module>
import sre_compile
File "pythonLib\Lib\sre_compile.py", line 15, in <module>
from sre_constants import *
File "pythonLib\Lib\sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
2.7.0报错如下

java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
at org.python.core.PyString.<init>(PyString.java:64)
at org.python.core.PyString.<init>(PyString.java:70)
...全文

相关推荐

评论
收藏 已收藏
分享
返回顶部
evezero2
Java 51,407 成员 86,128 内容