7

I'm new to python, and I've install Jython2.7.0

Java

import org.python.util.PythonInterpreter;
import org.python.core.*; 

public class Main {
    public static void main(String[] args) {
         PythonInterpreter interp = new PythonInterpreter(); 
         interp.execfile("D:/Users/JY/Desktop/test/for_java_test.py");  
         interp.close();
    }
}

Python

import pandas as pd
import ctypes

def main():
    data = pd.read_csv('for_test.csv')
    data_mean = data.a*2
    data_mean.to_csv('catch_test.csv',index=False)
    ctypes.windll.user32.MessageBoxW(0, "Done. Output: a * 2", "Output csv", 0)

if __name__ == '__main__':
    main()

Then I got this error.

Exception in thread "main" Traceback (most recent call last):
File "D:\Users\JYJU\Desktop\test_java\for_java_test.py", line 1, in <module>
    import pandas as pd
ImportError: No module named pandas

How can I fix this if I want to use pandas?

Thanos
  • 2,472
  • 1
  • 16
  • 33
Jimmy Chu
  • 83
  • 1
  • 1
  • 7
  • Could you please clarify why you're using Jython, instead of CPython? – farzad Mar 25 '16 at 09:09
  • And can you please provide more information about your setup? How did you install Jython and Pandas? More specifically, where did you install Pandas? – farzad Mar 25 '16 at 09:31
  • Because I wrote a python code to import .cvs and process data with pandas. Now my boss need me give it to user who's under java environment, but I rarely use java. So I choose jython to connect. – Jimmy Chu Mar 25 '16 at 10:02
  • I install `jython-installer-2.7.0.jar` , `JRE 7` , and I installed `pandas-0.17.1` on python 3.5 – Jimmy Chu Mar 25 '16 at 10:06
  • So you install pandas for Python (CPython). If you used `pip install pandas`, I'm guessing the `pip` command is the one from CPython. Under Jython bin directory, there is a `pip` script, if you call that one directly, it tries to install the module for Jython. I tried to install pandas like this and it failed, a sign that pandas is not available to be imported by Jython. – farzad Mar 25 '16 at 10:07
  • If your requirement is only to ship your software to customers (who might not have Python installed), and there is no strict requirement to integrate with Java libraries, then you can look at other solutions like py2exe or PyInstaller. – farzad Mar 25 '16 at 10:11
  • Thanks. I will try `.exe` way, if user agree. – Jimmy Chu Mar 26 '16 at 02:07
  • You cannot use Pandas directly in Jython yet, at least until JyNI is finished (if it ever finishes): http://jyni.org/ – Alex Huszagh May 08 '16 at 01:24

2 Answers2

9

You currently cannot use Pandas with Jython, because it depends on CPython specific native extensions. One dependency is NumPy, the other is Cython (which is actually not a native CPython extension, but generates such).

Keep an eye on the JyNI project ("Jython Native Interface"). It enables Jython to use native CPython-extensions and its exact purpose is to solve issues like that encountered by you. However, it is still under heavy development and not yet capable of loading Pandas or NumPy into Jython, but both frameworks are high on the priority list.

(E.g. ctypes is already working to some extend.)

Also, it is currently POSIX only (tested on Linux and OSX).

If you wouldn't require Jython specifically, but just some Java/Pandas interoperation, an already workable solution would be to embed the CPython interpreter. JPY and JEP are projects that provide this. With either of them you should be able to interoperate Java and Pandas (or any other CPython-specific framework).

stewori
  • 586
  • 4
  • 12
5

As far as I know pandas is written in cython and is a CPython extension. This means that it's meant to be used by CPython implementation of the Python language (which is the primary implemntation most people use).

Jython is a Python implementation to run Python programs on JVM and is used to provide integration with Java libraries, or Python scripting to Java programs, etc.

Python modules implemented as CPython extensions (like pandas) are not necessarily compatible with all Python implementations (famous implementations other than CPython are Jython, PyPy and IronPython)

If you really have to use Jython and pandas together and you could not find another way to solve the issue, then I suggest using them in different processes.

A Java process is your Jython application running on JVM (either is Java code calling Jython libraries, or a Python code that possibly requires integration with some Java libraries), and another CPython process runs to provide operations required from pandas.

Then use some form of IPC (or tool) to communicate (standard IO, sockets, OS pipes, shared memory, memcache, Redis, etc.).

The Java process sends a request to CPython (or registers the request to shared storage), providing processing parameters, CPython process uses pandas to calculate results and sends back a serialized form of the results (or puts the results back on the shared storage).

This approach requires extra coding (due to splitting the tasks into separate processes), and to serialize the request/response (which depends on the application and the data it's trying to process).

For example in this sample code on the question, Java process can provide the CSV filename to CPython, CPython processes the CSV file using pandas, generates the result CSV file and returns the name of the new file to Java process.

farzad
  • 8,775
  • 6
  • 32
  • 41