问题描述
我刚开始在我的Java类中使用 PythonInterpreter
,它运行得很好!但是,如果我尝试包含python模块( re
, HTMLParser
等),我收到以下内容异常(对于 re
):
I've just started using the PythonInterpreter
from within my Java classes, and it works great! However, if I try to include python modules (re
, HTMLParser
, etc.), I'm receiving the following exception (for re
):
Exception in thread "main" Traceback (innermost last):
File "", line 1, in ?
ImportError: no module named re
我如何从jython jar中创建类看到python可用的模块?
How could I make the classes from the jython jar "see" the modules python has available?
推荐答案
你嵌入了jython,你会使用一些Python模块:
You embed jython and you will use some Python-Modules somewere:
如果要在Java代码中设置路径(sys.path):
if you want to set the path (sys.path) in your Java-Code :
public void init() {
interp = new PythonInterpreter(null, new PySystemState());
PySystemState sys = Py.getSystemState();
sys.path.append(new PyString(rootPath));
sys.path.append(new PyString(modulesDir));
}
Py在org.python.core中。
Py is in org.python.core.
rootPath和modulesDir就是你想要的地方!
rootPath and modulesDir is where YOU want !
让rootPath指向你所在的标准-jython-lib
let rootPath point where you located the standard-jython-lib
看一下Jython-Source-Code中的src / org / python / util / PyServlet.java,例如
Have a look at src/org/python/util/PyServlet.java in the Jython-Source-Code for example
这篇关于Jython和python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!