-
Notifications
You must be signed in to change notification settings - Fork 135
Closed
Description
In my Java project (which uses graalvm polyglot) I want to use a Python script which depends on the requests
library - so I want to install it using pip.
Not sure if this is right place to post this, however I've not found a place for polyglot
This is what I tried so far:
version 1
public static void installModule(Context context, String module) {
context.eval("python", "import os\n" // line 32
+ "import pip\n"
+ "def install(package):\n"
+ " if hasattr(pip, 'main'):\n"
+ " pip.main(['install', package])\n"
+ " else:\n"
+ " pip._internal.main(['install', package])\n"
+ "install('"+ module +"')");
}
version 2
public static void installModule(Context context, String module) {
context.eval("python", "import subprocess\n" // line 32
+ "import sys\n"
+ "def install(package):\n"
+ " subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", package])\n"
+ "install('"+ module +"')");
}
In version 1 I got this error:
Exception in thread "main" ModuleNotFoundError: No module named 'pip'
at <python> <module>(Unknown)
at org.graalvm.polyglot.Context.eval(Context.java:428)
at uk.minersonline.gns34j.PythonConnector.installModule(PythonConnector.java:32)
at uk.minersonline.gns34j.PythonConnector.main(PythonConnector.java:16)
In version 2 I got this error:
Exception in thread "main" OSError: (5, I/O error)
at <python> __init__(C:\Users\samro\AppData\Local\org.graalvm.polyglot\python\python-home\7d0c656f1dac055b3b202d151a09908bf97221ff\Lib\subprocess.py:1687-1880:64732-73792)
at <python> call(Unknown)
at <python> check_call(Unknown)
at <python> install(Unknown)
at <python> <module>(Unknown)
at org.graalvm.polyglot.Context.eval(Context.java:428)
at uk.minersonline.gns34j.PythonConnector.installModule(PythonConnector.java:32)
at uk.minersonline.gns34j.PythonConnector.main(PythonConnector.java:16)
This is how I am calling both versions:
public static void main(String[] args) {
Context context = Context.newBuilder().allowAllAccess(true).build();
installModule(context,"requests"); # line 16
}
What I want to know is what is the best way to install a Python module from Java.
Metadata
Metadata
Assignees
Labels
No labels