I want to execute a Python function which is located in one of my python projects from java by using jython. https://smartbear.com/blog/test-and-monitor/embedding-jython-in-java-applications/ is giving the sample code for the purpose. But in my scenario I got the following exception.
Exception in thread "main" Traceback (most recent call last): File "", line 1, in ImportError: No module named JythonTestModule
My scenario is as follows.
I have created a python module inside my python project(pythonDev) by using PyCharm(JythonTestModule.py) which contains the following function.
def square(value): return value*value
Then I created a sample java class in my java project(javaDev) and called the python module.
public static void main(String[] args) throws PyException{ PythonInterpreter pi = new PythonInterpreter(); pi.exec("from JythonTestModule import square"); pi.set("integer", new PyInteger(42)); pi.exec("result = square(integer)"); pi.exec("print(result)"); PyInteger result = (PyInteger)pi.get("result"); System.out.println("result: "+ result.asInt()); PyFunction pf = (PyFunction)pi.get("square"); System.out.println(pf.__call__(new PyInteger(5))); }
After running this java method the aforementioned exception is generated by the java program. I want to know what is the problem with this menioned code segments.