bcdev / jpy

A bi-directional Python-Java bridge used to embed Java in CPython or the other way round.
Apache License 2.0
187 stars 37 forks source link

Error: function or method not found #173

Open luidicorra opened 4 years ago

luidicorra commented 4 years ago

Hello, i'm new to JPY, and wanted to try it for execute python code from a java project. So i just made a new Java project on Eclipse, and added a test.py python module that contains a method square:

test.py

class MyPlugin:
  def square(x):
    y = x * x;
    return y;

Then i've add two java classes:

MyPlugin.java

package it.testjpy;

public interface MyPlugin {
    int square(int x);
}

app.java

package it.testjpy;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.jpy.PyInputMode;
import org.jpy.PyLib;
import org.jpy.PyModule;
import org.jpy.PyObject;

public class app {

    public static void main(String[] args) {
        // Prepare required system properties like 'jpy.jpyLib' and others
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("C:\\Users\\luidicorra\\Documents\\jpy\\build\\lib.win-amd64-3.8\\jpyconfig.properties"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        properties.forEach((k, v) -> System.setProperty((String) k, (String) v));

    PyLib.startPython();
    PyObject.executeCode("print('Hello!')", PyInputMode.SCRIPT);

    // Proxify the call to a python class
        PyModule plugInModule = PyModule.importModule("test");
        PyObject plugInObj = plugInModule.call("MyPlugin");
        MyPlugin plugIn = plugInObj.createProxy(MyPlugin.class);

        int result = plugIn.square(5);

        System.out.println(result);

    PyLib.stopPython();

    }
}

The output is:

org.jpy.PyLib: entered static initializer org.jpy.PyLibConfig: entered static initializer org.jpy.PyLibConfig: exited static initializer org.jpy.PyLib: System.load("C:\Users\luidicorra\AppData\Local\Programs\Python\Python38\python38.dll") org.jpy.PyLib: System.load("C:\Users\luidicorra\eclipse-workspace-enterprise\JPY_Test\build\lib.win-amd64-3.8\jpy.cp38-win_amd64.pyd") org.jpy.PyLib: exited static initializer org.jpy.PyLib: Starting Python with 1 extra module path(s): org.jpy.PyLib: C:\Users\luidicorra\eclipse-workspace-enterprise\JPY_Test\build\lib.win-amd64-3.8 PyLib_startPython: entered: jenv=000000000305E1F8, pyInit=0, JPy_Module=0000000000000000 PyLib_startPython: global Python interpreter information: Py_GetProgramName() = "python" Py_GetPrefix() = "C:\Users\luidicorra\AppData\Local\Programs\Python\Python38" Py_GetExecPrefix() = "C:\Users\luidicorra\AppData\Local\Programs\Python\Python38" Py_GetProgramFullPath() = "C:\Program Files\Java\jre1.8.0_251\bin\javaw.exe" Py_GetPath() = "C:\Users\luidicorra\AppData\Local\Programs\Python\Python38\python38.zip;C:\Users\luidicorra\AppData\Local\Programs\Python\Python38\Lib\;C:\Users\luidicorra\AppData\Local\Programs\Python\Python38\DLLs\;C:\Program Files\Java\jre1.8.0_251\bin" Py_GetPythonHome() = "(null)" Py_GetVersion() = "3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)]" Py_GetPlatform() = "win32" Py_GetCompiler() = "[MSC v.1924 64 bit (AMD64)]" Py_GetBuildInfo() = "tags/v3.8.3:6f8c832, May 13 2020, 22:37:02" PyLib_startPython: exiting: jenv=000000000305E1F8, pyInit=1, JPy_Module=000000002066DEF0 Java_org_jpy_PyLib_executeCode: code='print('Hello!')' Java_org_jpy_PyLib_executeInternal: using main globals Java_org_jpy_PyLib_executeInternal: using globals for locals Hello! Java_org_jpy_PyLib_importModule: name='test' PyLib_CallAndReturnObject: objId=00000000207ED220, isMethodCall=0, name='MyPlugin', argCount=0 PyLib_CallAndReturnObject: error: function or method not found: 'MyPlugin' Exception in thread "main" java.lang.RuntimeException: Error in Python interpreter: Type: <class 'AttributeError'> Value: module 'test' has no attribute 'MyPlugin' Line: Namespace: File: at org.jpy.PyLib.callAndReturnObject(Native Method) at org.jpy.PyObject.call(PyObject.java:391) at it.testjpy.app.main(app.java:32)

Somebody can help me understand where i'm wrong? I've already looked on other issue threads and searched on google for anything related to my problem, but got nothing and even after many tryes i always end up with the same error.

Edit: If needed here's my project structure

C:\USERS\LUIDICORRA\ECLIPSE-WORKSPACE-ENTERPRISE\JPY_TEST
|   .classpath
|   .project
|   .pydevproject
|   
+---.settings
|       org.eclipse.jdt.core.prefs
|       
+---bin
|   |   test.py
|   |   
|   \---it
|       \---testjpy
|               app.class
|               MyPlugin.class
|               
+---build
|   +---bdist.win-amd64
|   +---lib.win-amd64-3.8
|   |       jdl.cp38-win_amd64.pyd
|   |       jpy-0.10.0-SNAPSHOT.jar
|   |       jpy.cp38-win_amd64.pyd
|   |       jpyconfig.properties
|   |       jpyconfig.py
|   |       jpyutil.py
|   |       
|   \---temp.win-amd64-3.8
|       \---Release
|           \---src
|               \---main
|                   \---c
|                       |   jpy.cp38-win_amd64.exp
|                       |   jpy.cp38-win_amd64.lib
|                       |   jpy_compat.obj
|                       |   jpy_conv.obj
|                       |   jpy_diag.obj
|                       |   jpy_jarray.obj
|                       |   jpy_jfield.obj
|                       |   jpy_jmethod.obj
|                       |   jpy_jobj.obj
|                       |   jpy_jtype.obj
|                       |   jpy_module.obj
|                       |   jpy_verboseexcept.obj
|                       |   
|                       \---jni
|                               jdl.cp38-win_amd64.exp
|                               jdl.cp38-win_amd64.lib
|                               org_jpy_DL.obj
|                               org_jpy_PyLib.obj
|                               
+---Python
|       test.py
|       
\---src
    \---it
        \---testjpy
                app.java
                MyPlugin.java