0

I'm trying to execute Python script from java and returning some value from Script to java.

here is my java code CallPY.java:

Properties properties = System.getProperties();

        properties.put("python.path", "/home/abcd/Downloads/");
        PythonInterpreter.initialize(System.getProperties(), properties, new String[0]);
        this.interpreter = new PythonInterpreter();
        this.interpreter.execfile("/home/abcd/Downloads/hello.py");
        PyInstance hello = ie.createClass("Hello", "None");
            PyObject result = hello.invoke("def_Name",
                    new PyString("value_1"), new PyString("value_2"));
        

and

my Python code is hello.py:

import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')
import pandas as pd
import numpy as np
import matplotlib
class Hello:  
    __gui = None  
    def __init__(self, gui):  
        self.__gui = gui  
    def def_Name(self, value_1, value_):
    s={"Key" : "Value"}
    print value_1, " ", value_2
    return s

I need numpy, panda and matplotlib for future needs and some other modules as well. While executing java file I'm getting error as:

 import pandas as pd
  File "/usr/local/lib/python2.7/dist-packages/pandas/__init__.py", line 14
    except ImportError as e:
                      ^
SyntaxError: mismatched input 'as' expecting COLON

There are no issues while running the Python script from console and Jupyter, so there is same issue with java code. Please point me how to fix the issue.

Thanks in Advance.

Community
  • 1
  • 1
arpan
  • 119
  • 2
  • 8
  • I think you are running wrong python version – Arpit Solanki Feb 20 '18 at 11:42
  • The `except Exception as exc` syntax was added in Python 2.6. That makes it unlikely that you really are using Jython 2.7. Also note that NumPy does not work with Jython directly. See https://www.scipy.org/scipylib/faq.html#does-numpy-scipy-work-with-jython. You might be able to get it to work with the help of JyNI: http://jyni.org/. – mzjn Feb 20 '18 at 12:12
  • I'm using Python 2.7 and added dependency for Jython2.7.0. `I'm just running a script from java, isn't it right using numpy in Python script?` – arpan Feb 20 '18 at 12:57
  • I have added maven dependency . I'm new to python and jython so I don't how to work with them. All I want is to resolve the issue and run a python function from java and get a value returned by the function. – arpan Feb 21 '18 at 05:11
  • The SyntaxError is explained in the answer to this old question: https://stackoverflow.com/q/3020966/407651. If you manage to get rid of that error, you will get other errors instead because NumPy is not compatible with Jython (which I have already pointed out). – mzjn Feb 21 '18 at 17:34
  • Thanks for suggestion I'm working on it. For now I've removed it and tried to import `from elasticsearch import Elasticsearch` but I'm getting error as ` File "/usr/local/lib/python2.7/dist-packages/elasticsearch/__init__.py", line 1, in from __future__ import absolute_import ImportError: No module named __future__` – arpan Feb 22 '18 at 07:04
  • If you need help with elasticsearch, please ask a new question. – mzjn Feb 22 '18 at 15:25
  • Elasticsearch was not an issue but I was just confirming that is there anything wrong with java code? – arpan Feb 26 '18 at 04:24

0 Answers0