gpt4 book ai didi

java - 从 Java 调用 python 模块时出错

转载 作者:行者123 更新时间:2023-12-01 09:38:56 27 4
gpt4 key购买 nike

调用Python的Java代码:

//arguments to be passed to the script
String[] patchArguments = { patchFileDirectory,centralPatchStagePath,patchId,patchFileName, action };
//initialize the interpreter with properties and arguments
PythonInterpreter.initialize(System.getProperties(), System.getProperties(), patchArguments);
pythonInterpreter = new PythonInterpreter();
//invoke python interpreter to execute the script
pythonInterpreter.execfile(opatchScriptPath + opatchScript);

回溯(最内层最后):

File "/scratch/app/product/fmw/obpinstall/patching/scripts/PatchUtility.py", line 4, in ?

ImportError: no module named subprocess

但是子进程已经安装,并且如果我直接使用终端执行python文件,它就会运行python PatchUtility.py

更新:我发现了一些东西

Jython has some limitations:

There are a number of differences. First, Jython programs cannot use CPythonextension modules written in C. These modules usually have files with theextension .so, .pyd or .dll.

子进程内部调用 C 扩展吗?

最佳答案

简而言之:不。或者也许。或者是。 但与您最相关的是 Jython,No。

TLDR:Jython 有自己的子流程实现

Python 文档中的详细信息有点粗略,但是 PEP 有更多详细信息 ( https://www.python.org/dev/peps/pep-0324/ )。这是关于它应该如何工作的规范,而不是实际的实现:Python 的实现可以做任何它喜欢的事情,只要它的功能相同(这好吧,使它不是“随心所欲”,但是......明白了)。

来自规范:

  • On POSIX platforms, no extension module is required: the module uses os.fork(), os.execvp() etc.
  • On Windows platforms, the module requires either Mark Hammond's Windows extensions[5], or a small extension module called _subprocess.

子进程 PEP 旨在防止使用 os.popen 时发生的奇怪情况。类型函数,但是我还在 Jython 文档中注意到,这是为 jython、os.fork 和整个子进程模块本身实现的:http://www.jython.org/docs/library/subprocess.html

我怀疑您在某个地方还有另一个错误,也许是一个导入错误,这使得它看起来像是无法导入的子进程。

您提到的 C 模块更多地是关于自定义 python C 模块。这些不起作用,因为它们与 python 函数绑定(bind),而 jython 使用 Java 位实现其内部结构。该语言提供的所有核心功能都必须移植到 Java 才能使 Java 交互工作。

关于java - 从 Java 调用 python 模块时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38606613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com