Search code examples
javapythonprocessbuilder

Python Running under Java ProcessBuilder cannot not find pandas import


I am running a python process in Java using ProcessBuilder. For now it is a simple test program that prints out a before statement than imports pandas as pd and then prints out another statement. It's finishing with the error

import pandas as pd ImportError: No module named pandas

Pandas is installed. Java and ProcessBuilder Just can't find them.

def main():
    print("hello world before")
    import pandas as pd
    df = pd.DataFrame()
    print("after the dataframe")


main()
public class runPythonTest {

  public static void main(String[] args) {

    try{
      ArrayList<String> list = new ArrayList<>();
      list.add("python");
      list.add("/Users/loisgh/PycharmProjects/pythontest/python_test.py");
      ProcessBuilder pb = new ProcessBuilder(list);
      Process p = pb.start();
      BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
      BufferedReader readerError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
      String line = null;
      while ( (line = reader.readLine()) != null) {
        System.out.println(line);
      }
      while ( (line = readerError.readLine()) != null) {
        System.out.println(line);
      }

    }catch(Exception e){System.out.println(e);}
  }

}

Can some tell me how to fix this problem and reveal the location of pandas to java.


Solution

  • When you use ProcessBuilder, it doesn't take your PATH environment variable into account, which breaks imports. To fix this, you have to provide the full path to ProcessBuilder, so change this:

    list.add("python");
    

    to the full path to where your python executable is located. Try using which python to find it if you're on linux, or where python if you're on Windows. You should end up with something like:

    list.add("users/path/2.7/python");
    

    Also, if you have multiple versions of Python installed (e.g. 2.7 and 3.7), make sure you're referring to the correct one, or that both have the module installed.