static Process startPythonProcess(
            PythonEnvironment pythonEnv, List<String> commands, boolean redirectToPipe)
            throws IOException {
        ProcessBuilder pythonProcessBuilder = new ProcessBuilder();
        Map<String, String> env = pythonProcessBuilder.environment();
        if (pythonEnv.pythonPath != null) {
            String defaultPythonPath = env.get("PYTHONPATH");
            if (Strings.isNullOrEmpty(defaultPythonPath)) {
                env.put("PYTHONPATH", pythonEnv.pythonPath);
            } else {
                env.put(
                        "PYTHONPATH",
                        String.join(File.pathSeparator, pythonEnv.pythonPath, defaultPythonPath));
            }
        }
        if (pythonEnv.archivesDirectory != null) {
            pythonProcessBuilder.directory(new File(pythonEnv.archivesDirectory));
        }
        pythonEnv.systemEnv.forEach(env::put);
        commands.add(0, pythonEnv.pythonExec);
        pythonProcessBuilder.command(commands);
        // redirect the stderr to stdout
        pythonProcessBuilder.redirectErrorStream(true);
        if (redirectToPipe) {
            pythonProcessBuilder.redirectOutput(ProcessBuilder.Redirect.PIPE);
        } else {
            // set the child process the output same as the parent process.
            pythonProcessBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
        }

        LOG.info(
                "Starting Python process with environment variables: {{}}, command: {}",
                env.entrySet().stream()
                        .map(e -> e.getKey() + "=" + e.getValue())
                        .collect(Collectors.joining(", ")),
                String.join(" ", commands));
        Process process = pythonProcessBuilder.start();
        if (!process.isAlive()) {
            throw new RuntimeException("Failed to start Python process. ");
        }

        return process;
    }
@Test
    void testStartPythonProcess() {
        PythonEnvUtils.PythonEnvironment pythonEnv = new PythonEnvUtils.PythonEnvironment();
        pythonEnv.tempDirectory = tmpDirPath;
        pythonEnv.pythonPath = tmpDirPath;
        List<String> commands = new ArrayList<>();
        String pyPath = String.join(File.separator, tmpDirPath, "verifier.py");
        try {
            File pyFile = new File(pyPath);
            pyFile.createNewFile();
            pyFile.setExecutable(true);
            String pyProgram =
                    "#!/usr/bin/python\n"
                            + "# -*- coding: UTF-8 -*-\n"
                            + "import os\n"
                            + "import sys\n"
                            + "\n"
                            + "if __name__=='__main__':\n"
                            + "\tfilename = sys.argv[1]\n"
                            + "\tfo = open(filename, \"w\")\n"
                            + "\tfo.write(os.getcwd())\n"
                            + "\tfo.close()";
            Files.write(pyFile.toPath(), pyProgram.getBytes(), StandardOpenOption.WRITE);
            String result = String.join(File.separator, tmpDirPath, "python_working_directory.txt");
            commands.add(pyPath);
            commands.add(result);
            Process pythonProcess = PythonEnvUtils.startPythonProcess(pythonEnv, commands, false);
            int exitCode = pythonProcess.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Python process exits with code: " + exitCode);
            }
            String cmdResult = new String(Files.readAllBytes(new File(result).toPath()));
            // Check if the working directory of python process is the same as java process.
            assertThat(cmdResult).isEqualTo(System.getProperty("user.dir"));
            pythonProcess.destroyForcibly();
            pyFile.delete();
            new File(result).delete();
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException("test start Python process failed " + e.getMessage());
        }
    }