Search code examples
javaeclipsepython-2.7pydev

Using python pandas module in Eclipse Mars


I am combining java and python code in Eclipse and when I want to execute python script that contains pandas module called from java class I get null value. Otherwise, if I am not using pandas module, but just simple python script everything works fine. I assume PyDev interpreter properly and paths to all python modules are configured properly since I do not get any import error and I can get an module info when I cover mouse over it in the code.

All modules are in /usr/local/lib/python2.7/site-packages.

Please check java class, python script and PyDev configurations:

package test4;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class test4 {

public static void main(String a[]){
    try{

    ProcessBuilder pb = new ProcessBuilder("/usr/local/bin/python2.7","solinor_final.py");
    Process p = pb.start();
    System.out.println("Hello");
    String line = null;
    StringBuilder sb = new StringBuilder(); 
    BufferedReader in = null;

    try {
        in = new BufferedReader(new InputStreamReader(p.getInputStream()));

        while((line=in.readLine())!=null) {
            sb.append(line);
            System.out.println(line);

        }


    } finally {

        in.close();
    }


    System.out.println("value is : "+sb);

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

}

Python:

    import sys

    sys.path.append("/usr/local/bin")
    sys.path.append("/usr/bin/")
       sys.path.append('/Users/quuppa/Documents/workspace/test4/report1.csv')


import pandas as pd   


def main():
    import python_class as so
    print "Hello inside"

    data = pd.read_csv("report1.csv",sep=",",header=None)
    data = data.rename(columns={0:'Merchant name',1:'Business ID',2:'Main merchant ID',3:'Report type',4:'Report search date',5:'Report period',\
                            6:'Outlet name',7:'Address',8:'Number of transactions',9:'Value of transactions',10:'Commission',11:'Chargebacks and adjustments',12:'Settlement value'})

    data = data.drop([data.index[0]])

    bd = so.Solinor(data)

    total_amount = bd.totalAmount()
    print total_amount

if __name__ == "__main__":
    main()

These are the outputs with and without including pandas module:

Hello
value is :

Hello
Hello inside
value is : Hello inside

Configuration image:

enter image description here

Do you have any suggestion what could be a problem in running the code with pandas module?

Thanks!


Solution

  • I find the solution for this problem. The most efficient way is just to use ProcessBuilder without PyDev and PYTHONPATH settings. The necessary modifications are to add in ProcessBuilder first argument to be full path to python installation, in my case this is in /usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/bin/python2.7, and second argument to be full path to main python script.

    The second modification is to place python scripts outside Eclipse workspace and pass the full path as a second argument in ProcessBuilder to the main python script. I assume that PyDev somehow has a problem in reading libraries written in C such as numpy.