如何在Java中使用Jython Standalone安装Python库?

时间:2017-04-29 19:59:55

标签: java pip jython

我正在尝试使用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帖子:

Installing python module within code

How can I install various Python libraries in Jython?

我正在运行Java 1.8,Gradle 2.14.1,Jython Standalone 2.7.1b3和Ubuntu 16.04

0 个答案:

没有答案