简体   繁体   English

Jython和python模块

[英]Jython and python modules

I've just started using the PythonInterpreter from within my Java classes, and it works great! 我刚开始在我的Java类中使用PythonInterpreter ,它工作得很好! However, if I try to include python modules ( re , HTMLParser , etc.), I'm receiving the following exception (for re ): 但是,如果我尝试包含python模块( reHTMLParser等),我收到以下异常(对于re ):

Exception in thread "main" Traceback (innermost last):
  File "", line 1, in ?
ImportError: no module named re

How could I make the classes from the jython jar "see" the modules python has available? 我怎样才能让jython jar中的类“看到”python可用的模块?

You embed jython and you will use some Python-Modules somewere: 你嵌入了jython,你会使用一些Python模块:

if you want to set the path (sys.path) in your Java-Code : 如果要在Java代码中设置路径(sys.path):

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 is in org.python.core. Py在org.python.core中。

rootPath and modulesDir is where YOU want ! rootPath和modulesDir是你想要的!

let rootPath point where you located the standard-jython-lib 让rootPath指向您找到standard-jython-lib的位置

Have a look at src/org/python/util/PyServlet.java in the Jython-Source-Code for example 看一下Jython-Source-Code中的src / org / python / util / PyServlet.java,例如

According to the FAQ : 根据FAQ

4.1 What parts of the Python library are supported? 4.1支持Python库的哪些部分?

The good news is that Jython now supports a large majority of the standard Python library. 好消息是Jython现在支持大多数标准Python库。 The bad news is that this has moved so rapidly, it's hard to keep the documentation up to date. 坏消息是,这已经发生了如此迅速的变化,很难保持文档的最新状态。

Built-in modules (eg those that are written in C for CPython) are a different story. 内置模块(例如用C语言编写的CPython模块)是另一回事。 These would have to be ported to Java, or implemented with a JNI bridge in order to be used by Jython. 这些必须移植到Java,或者用JNI桥实现,以供Jython使用。 Some built-in modules have been ported to JPython, most notably cStringIO, cPickle, struct, and binascii. 一些内置模块已移植到JPython,最着名的是cStringIO,cPickle,struct和binascii。 It is unlikely that JNI modules will be included in Jython proper though. JNI模块不太可能包含在Jython中。

If you want to use a standard Python module, just try importing it. 如果您想使用标准Python模块,只需尝试导入它。 If that works, you're probably all set. 如果有效,你可能都已经完成了。 You can also do a dir() on the modules to check the list of functions it implements. 您还可以在模块上执行dir()以检查它实现的函数列表。

If there is some standard Python module that you have a real need for that doesn't work with Jython yet, please send us mail. 如果有一些您真正需要的标准Python模块尚未与Jython一起使用,请发送邮件给我们。

In other words, you can directly use Python modules from Jython, unless you're trying to use built-in modules, in which case you're stuck with whatever has been ported to Jython. 换句话说,您可以直接使用Jython中的Python模块,除非您尝试使用内置模块,在这种情况下,您将无法使用已移植到Jython的任何内容。

Check your jython sys.path . 检查你的jython sys.path。 Make sure that the library you want to load are in this path. 确保要加载的库位于此路径中。 Look at jython faq for more details. 查看jython faq以获取更多详细信息。

You can refer here for the solution Importing python modules in jython 您可以在这里参考解决方案在jython中导入python模块

Download ez_setup.py from here http://peak.telecommunity.com/dist/ez_setup.py http://peak.telecommunity.com/dist/ez_setup.py下载ez_setup.py

Then run jython ez_setup.py <any module name> . 然后运行jython ez_setup.py <any module name>

Running it on any folder path doesn't matter. 在任何文件夹路径上运行都无关紧要。

I could install pymysql with it, no problem. 我可以用它安装pymysql,没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM