I am working with a Python library but everything else is in Java. I want to be able to access and use the Python library from Java, so I started researching and using Jython. I need to use numpy and neurokit libraries.
I write this simple code in Java:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.set("values", 10 );
interpreter.execfile("D:\\PyCharmWorkspace\\IoTproject\\Test.py");
PyObject b = interpreter.get("result");
and the code in Python:
import sys
sys.path.append("D:\\PyCharmWorkspace\\venv\\lib\\site-packages")
import numpy as np
result = values + 20
The problem is that when It tries to load module numpy, I get this error:
Exception in thread "main" Traceback (most recent call last):
File "D:\PyCharmWorkspace\IoTproject\TestECGfeature.py", line 4, in <module>
import numpy as np
File "D:\PyCharmWorkspace\venv\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import core
File "D:\PyCharmWorkspace\venv\lib\site-packages\numpy\core\__init__.py", line 24, in <module>
from . import multiarray
File "D:\PyCharmWorkspace\venv\lib\site-packages\numpy\core\__init__.py", line 24, in <module>
from . import multiarray
File "D:\PyCharmWorkspace\venv\lib\site-packages\numpy\core\multiarray.py", line 14, in <module>
from . import overrides
File "D:\PyCharmWorkspace\venv\lib\site-packages\numpy\core\overrides.py", line 166
SyntaxError: unqualified exec is not allowed in function 'decorator' because it contains free variables
I also tried to do this:
interpreter.exec("import sys");
interpreter.exec("sys.path.append('D:\\PyCharmWorkspace\\venv\\lib\\site-packages')");
interpreter.exec("import numpy as np");
and I get:
Exception in thread "main" Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named numpy
To install Jython I have add jar file to project build-path.
I found jep and jpy that can make communicate java with python but I didn't found how to install or use them.
What I need is call a Python function giving params and getting result. How Can I do or How can I solve the problem using Jython?