Skip to content

Commit

Permalink
[python] Support pip install in offline mode (deepjavalibrary#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu authored May 18, 2023
1 parent f12535a commit 7a4ab77
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions engines/python/src/main/java/ai/djl/python/engine/PyEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -167,17 +168,27 @@ public synchronized void installDependency(Path modelDir) {
}
Path file = modelDir.resolve("requirements.txt");
if (Files.isRegularFile(file)) {
String[] cmd = {
pythonExecutable,
"-m",
"pip",
"-q",
"install",
"-r",
file.toAbsolutePath().toString()
};
List<String> cmd = new ArrayList<>(9);
cmd.add(pythonExecutable);
cmd.add("-m");
cmd.add("pip");
if (!logger.isDebugEnabled()) {
cmd.add("-q");
}
cmd.add("install");
cmd.add("-r");
cmd.add(file.toAbsolutePath().toString());
if (Boolean.getBoolean("offline")) {
cmd.add("--no-deps");
}
Path dir = modelDir.resolve("requirements");
if (Files.isDirectory(dir)) {
cmd.add("-f");
cmd.add(file.toAbsolutePath().toString());
}
try {
logger.info("Found requirements.txt, start installing Python dependencies...");
logger.debug("{}", cmd);
Process process = new ProcessBuilder(cmd).redirectErrorStream(true).start();
String logOutput;
try (InputStream is = process.getInputStream()) {
Expand All @@ -186,10 +197,10 @@ public synchronized void installDependency(Path modelDir) {
int ret = process.waitFor();
if (ret == 0) {
logger.info("pip install requirements succeed!");
logger.debug(logOutput);
logger.debug("{}", logOutput);
} else {
logger.warn("requirements installation failed! With error code: {}", ret);
logger.warn(logOutput);
logger.warn("pip install failed with error code: {}", ret);
logger.warn("{}", logOutput);
}
} catch (IOException | InterruptedException e) {
logger.warn("pip install requirements failed.", e);
Expand Down

0 comments on commit 7a4ab77

Please sign in to comment.