在 Java 中使用 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1164810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:26:30  来源:igfitidea点击:

Using Python from within Java

javapythonjythonrpc

提问by Liam

Possible Duplicate:
Java Python Integration

可能的重复:
Java Python 集成

I have a large existing codebase written in 100% Java, but I would like to use Python for some new sections of it. I need to do some text and language processing, and I'd much rather use Python and a library like NLTKto do this.

我有一个 100% 用 Ja​​va 编写的大型现有代码库,但我想将 Python 用于其中的一些新部分。我需要做一些文本和语言处理,我更愿意使用 Python 和像NLTK这样的库来做这件事。

I'm aware of the Jython project, but it looks like this represents a way to use Java and its libraries from within Python, rather than the other way round - am I wrong about this?

我知道 Jython 项目,但看起来这代表了一种在 Python 中使用 Java 及其库的方法,而不是相反 - 我错了吗?

If not, what would be the best method to interface between Java and Python, such that (ideally) I can call a method in Python and have the result returned to Java?

如果不是,那么在 Java 和 Python 之间进行接口的最佳方法是什么,这样(理想情况下)我可以在 Python 中调用一个方法并将结果返回给 Java?

Thank you.

谢谢你。

采纳答案by Michael Borgwardt

I'm aware of the Jython project, but it looks like this represents a way to use Java and its libraries from within Python, rather than the other way round - am I wrong about this?

我知道 Jython 项目,但看起来这代表了一种在 Python 中使用 Java 及其库的方法,而不是相反 - 我错了吗?

Yes, you are wrong. You can either call a command line interpreterto run python code using Jyton or use python code from Java. In the past there was also a python-to-Java compiler, but it got discontinued with Jython 2.2

是的,你错了。您可以调用命令行解释器来使用 Jyton 运行 python 代码,也可以使用 Java 中的 python 代码。过去也有一个 python-to-Java 编译器,但它在 Jython 2.2 中停产了

回答by S.Lott

Simply run the Python interpreter as a subprocess from within Java.

只需在 Java 中将 Python 解释器作为子进程运行即可。

Write your Python functionality as a proper script, which reads from stdin and writes to stdout.

将您的 Python 功能编写为适当的脚本,该脚本从标准输入读取并写入标准输出。

Use the Java Runtimeclass to spawn a subprocess that runs your Python script. This is very simple to do and provides a very clean interface.

使用 JavaRuntime类生成一个运行 Python 脚本的子进程。这很容易做到,并提供了一个非常干净的界面。



Edit

编辑

import simplejson
import sys
for request in sys.stdin.readlines():
    args = simplejson.loads( request )
    result = myFunction( args['this'], args['that'] )
    sys.stdout.writeline( simplejson.dumps( result ) + "\n" )

The interface is simple, structured and very low overhead.

该界面简单、结构化且开销非常低。

回答by kdgregory

Jython is a Python implementation running on the JVM. You can find information about embedding Python in an existing Java app in the user guide.

Jython 是在 JVM 上运行的 Python 实现。您可以在用户指南中找到有关在现有 Java 应用程序中嵌入 Python 的信息。

I don't know the environment that you're working in, but be aware that mixing languages in the same app can quickly lead to a mess. I recommend creating Java interfaces to represent the operations that you plan to use, along with separately-packaged implementation classes that wrap the Python code.

我不知道您工作的环境,但请注意,在同一个应用程序中混合语言可能会很快导致混乱。我建议创建 Java 接口来表示您计划使用的操作,以及包装 Python 代码的单独打包的实现类。

回答by Mia Clarke

I would write a Python module to handle the text and language processing, and then build a small bridge in jython that your java program can interact with. The jython bridge will be a very simple one, that's really only responsible for forwarding calls to the python module, and return the answer from the python module to the java module. Jython is really easy to use, and setup shouldn't take you more than 15 minutes.

我会编写一个 Python 模块来处理文本和语言处理,然后在 jython 中构建一个您的 Java 程序可以与之交互的小桥。jython 桥接器将是一个非常简单的桥接器,它实际上只负责将调用转发到 python 模块,并将答案从 python 模块返回到 java 模块。Jython 真的很容易使用,设置不会超过 15 分钟。

Best of luck!

祝你好运!

回答by rob

IN my opinion, Jython is exactly what you are looking at.
It is an implementationof Python within the JVM; as such, you can freely exchange objects and, for instance, inherit from a Java class (with some limitations).

在我看来,Jython 正是您所看到的。
它是JVM 中 Python的实现;因此,您可以自由地交换对象,例如,从 Java 类继承(有一些限制)。

Note that, its major strength point (being on top of of JVM) is also its major drawback, because it cannot use all (C)Python extension written in C (or in any other compiled language); this may have an impact on what you are willing to do with your text processing.

请注意,它的主要优点(位于 JVM 之上)也是它的主要缺点,因为它不能使用所有用 C(或任何其他编译语言)编写的 (C)Python 扩展;这可能会影响您对文本处理的意愿。

For more information about what is Jython, its potential and its limitations, I suggest you reading the Jython FAQ.

有关 Jython 是什么、它的潜力和局限性的更多信息,我建议您阅读Jython 常见问题解答

回答by Carlos Santos

I don't think you could use NLTK from Jython, since it depends on Numpy which isn't ported to the JVM. If you need NLTK or any other native CPython extension, you might consider using some IPC mechanism to communicate between CPython and the JVM. That being said, there is a project to allow calling CPython from Java, called Jepp:

我认为您不能使用 Jython 的 NLTK,因为它依赖于未移植到 JVM 的 Numpy。如果您需要 NLTK 或任何其他本机 CPython 扩展,您可以考虑使用一些 IPC 机制在 CPython 和 JVM 之间进行通信。话虽如此,有一个项目允许从 Java 调用 CPython,称为 Jepp:

http://jepp.sourceforge.net/

http://jepp.sourceforge.net/

The reverse (calling Java code from CPython) is the goal of JPype and javaclass:

反过来(从 CPython 调用 Java 代码)是 JPype 和 javaclass 的目标:

sourceforge.net/projects/jpype/

sourceforge.net/projects/jpype/

pypi.python.org/pypi/javaclass/0.1

pypi.python.org/pypi/javaclass/0.1

I've never used any of these project, so I cant't vow for their quality.

我从未使用过这些项目中的任何一个,所以我不能保证它们的质量。

回答by Mikael Gueck

Remember to first check from those paying for the development that they're OK with the codebase needing a developer who knows both Python and Java from now on, and other cost and maintainability effects you've undoubtedly already accounted for.

请记住,首先要从那些支付开发费用的人那里检查他们是否同意代码库,从现在开始需要一个同时了解 Python 和 Java 的开发人员,以及您无疑已经考虑到的其他成本和可维护性影响。

See: http://www.acm.org/about/se-code1.06, 2.03, 2.09, 4.03, 4.05, 6.07

请参阅:http: //www.acm.org/about/se-code 1.06、2.03、2.09、4.03、4.05、6.07