pythonjavajythonsikuli

NameError: name 'ResourceWarning' is not defined


java version "1.8.0_311"

Python 3.10.8

Sikuli:-

<dependency>
            <groupId>sikulixapi</groupId>
            <artifactId>sikulixapi</artifactId>
            <scope>system</scope>
            <version>2.0.5</version>
            <systemPath>${project.basedir}/sikulixapi-2.0.5.jar</systemPath>
        </dependency>

I am getting the below error while running a sample python program from java.

Exception in thread "main" Traceback (most recent call last):
  File "foo\boo\sikulixapi-2.0.5.jar\Lib\site.py", line 68, in <module>
  File "foo\boo\sikulixapi-2.0.5.jar\Lib\os.py", line 64, in <module>
  File "foo\boo\sikulixapi-2.0.5.jar\Lib\ntpath.py", line 12, in <module>
  File "foo\boo\sikulixapi-2.0.5.jar\Lib\warnings.py", line 395, in <module>
  File "foo\boo\sikulixapi-2.0.5.jar\Lib\warnings.py", line 395, in <module>
  File "__pyclasspath__/_warnings.py", line 105, in <module>
NameError: name 'ResourceWarning' is not defined

Python:-

class Hello:
    __gui = None

    def __init__(self, gui):
        self.__gui = gui

    def run(self):
        print('Hello world!')

Java

package Types.UserInterface.Utilities;

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


public class InterpreterExample {

    PythonInterpreter interpreter = null;


public InterpreterExample() {
    PythonInterpreter.initialize(System.getProperties(),
            System.getProperties(), new String[0]);

    this.interpreter = new PythonInterpreter();
}

void execfile(final String fileName) {
    this.interpreter.execfile(fileName);
}

PyInstance createClass(final String className, final String opts) {
    return (PyInstance) this.interpreter.eval(className + "(" + opts + ")");
}

public static void main(String gargs[]) {
    InterpreterExample ie = new InterpreterExample();

    ie.execfile("hello.py");

    PyInstance hello = ie.createClass("Hello", "None");

    hello.invoke("run");
}
} 

Not sure why it is conflicting with sikuli classes while running the sample java program.

Note: Python program works perfectly fine when they are executed directly. Problem raises only when the same program is called in java.

Could someone give a direction as to what is the approach to fix this?


Solution

  • cf Learning how to use Jython - ResourceWarning error

    Jython currently only works with Python 2, you can't use Python 3 with it (for now), hence the error.