无法使用j在JYTHON中导入python模块

2024-04-19 22:39:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个java代码,其中使用jython执行python文件。对于默认的python依赖项,我工作得很好,但是当我提供一些额外的python模块时就不起作用了。Python代码在Python控制台中运行良好。在

java代码

PySystemState sys = Py.getSystemState();
sys.path.append(new PyString("/usr/local/lib/python2.7/dist-packages/"));
sys.path.append(new PyString("/usr/local/lib/python2.7/site-packages/"));

PythonInterpreter.initialize(System.getProperties(), System.getProperties(), arguments);
PythonInterpreter python = new PythonInterpreter(null, sys);
StringWriter out = new StringWriter();
python.setOut(out);
python.execfile("/home/lucy/input.py");

在输入.py在

^{pr2}$

错误

  import nltk
SyntaxError: ("mismatched input 'as' expecting COLON", ('/usr/local/lib/python2.7/dist-packages/nltk/__init__.py', 35, 15, 'except IOError as ex:\n'))

系统版本

'2.7.6 (default, Oct 26 2016, 20:30:19) \n[GCC 4.8.4]'

jython版本

<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.5.2</version>
</dependency>

Tags: path代码pynewlibpackagesusrlocal