我正在尝试使用 Java 中的 Jython Standalone jar 安装 Python 库。
我的代码似乎将 PIP 7.1.2 安装在独立 jar 旁边的文件夹中,但是一旦我尝试使用 PIP 进行自身升级,它就会失败并出现 ZipImportError。
错误消息似乎抱怨无法打开 PIP 轮文件,但日志似乎显示它已经能够打开它几次。
import org.python.util.PythonInterpreter;
public class PipInstaller{
public void installPip(){
PythonInterpreter python = new PythonInterpreter();
runEnsurePip(python);
upgradePip(python);
python.close();
}
private void runEnsurePip(PythonInterpreter python){
StringBuilder script = new StringBuilder();
script.append("import ensurepip\n");
script.append("ensurepip._main()");
python.exec(script.toString());
}
private void upgradePip(PythonInterpreter python){
StringBuilder script = new StringBuilder();
script.append("import pip\n");
script.append("pip.main(['install', '--upgrade', 'pip'])");
python.exec(script.toString());
}
}
这些是堆栈跟踪的最后几行:
File "/tmp/tmpnmI49K/pip-7.1.2-py2.py3-none-any.whl/pip/_vendor/html5lib/html5parser.py", line 27, in parse
File "/tmp/tmpnmI49K/pip-7.1.2-py2.py3-none-any.whl/pip/_vendor/html5lib/treebuilders/__init__.py", line 69, in getTreeBuilder
zipimport.ZipImportError: zipimport: can not open file: /tmp/tmpnmI49K/pip-7.1.2-py2.py3-none-any.whl
这些是我过去使用的 stackoverflow 帖子来了解我现在的情况:
在代码中安装 python 模块 https://stackoverflow.com/questions/12332975/installing-python-module-within-code
如何在 Jython 中安装各种 Python 库? https://stackoverflow.com/questions/6787015/how-can-i-install-various-python-libraries-in-jython/6787069#6787069
我正在运行 Java 1.8、Gradle 2.14.1、Jython Standalone 2.7.1b3 和 Ubuntu 16.04