问题
I am trying to install Python libraries using a Jython Standalone jar in Java.
The code I have seems to install PIP 7.1.2 in a folder next to the standalone jar, but once I try to use PIP to upgrade itself, it fails with a ZipImportError.
The error message seems to complain about not being able to open the PIP wheel file, but the log seems to show it being able to open it a few times already.
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());
}
}
These are the last couple lines of the stacktrace:
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
These are the stackoverflow posts I used to get where I am now:
Installing python module within code
How can I install various Python libraries in Jython?
I am running Java 1.8, Gradle 2.14.1, Jython Standalone 2.7.1b3, and Ubuntu 16.04
来源:https://stackoverflow.com/questions/43699968/how-can-i-install-python-libraries-using-jython-standalone-in-java