Jython importing a module by changing sys.path in a PyInterpreter and PySystemState still raises an import error

82
July 20, 2021, at 04:20 AM

Here's a very simple elaboration of said problem.

I have a single main.java script that is composed of the code followed;

// main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("\\\\ Interpreter \\\\");
        test_terpreter();
        System.out.println("\\\\ SystemState \\\\");
        test_systate();
    }
// test_terpreter
    public static void test_terpreter() {
         String package_path = "C:\\Users\\User\\Documents\\Programming\\Java\\far_away_package";
         PythonInterpreter interpreter = new PythonInterpreter();
         interpreter.exec(String.format("import sys; sys.path.append('%s');sys_path = sys.path;", package_path));
         System.out.format("SYSPATH IS: %s\n", interpreter.get("sys_path").toString());
         System.out.format("IN SPECIFIED PATH IS: %s\n", String.join(", ", new File(package_path).list()));
         try {
             interpreter.exec(String.format("import sys;sys.path.append('%s');import MyModule;", package_path));
         } catch (PyException e) {
             System.out.println("ImportError: No module named MyModule (BY INTERPRETER)");
         }
    }
// test_systate
    public static void test_systate() {
        String package_path = "C:\\Users\\User\\Documents\\Programming\\Java\\far_away_package";
        PySystemState sys = new PySystemState();
        sys.path.append(Py.newString(package_path));
        System.out.format("SYSPATH IS: %s\n", sys.path.toString());
        System.out.format("IN SPECIFIED PATH IS: %s\n", String.join(", ", new File(package_path).list()));
        PyObject importer = sys.getBuiltins().__getitem__(Py.newString("__import__"));
        try {
        PyObject module = importer.__call__(Py.newString("MyModule"));
        } catch (PyException e) {
        System.out.println("ImportError: No module named MyModule (BY SYSTATE)");
        }
    }
}

Given the test case above, when running the script, this is the sysdout,

\\ Interpreter \\
SYSPATH IS: ['C:\\jython2.7.2\\Lib', '__classpath__', '__pyclasspath__/', 'C:\\jython2.7.2\\Lib\\site-packages', 'C:\\Users\\User\\Documents\\Programming\\Java\\far_away_package']
IN SPECIFIED PATH IS: MyModule.py
ImportError: No module named MyModule (BY INTERPRETER)
\\ SystemState \\
SYSPATH IS: ['C:\\jython2.7.2\\Lib', '__classpath__', '__pyclasspath__/', 'C:\\Users\\User\\Documents\\Programming\\Java\\far_away_package']
IN SPECIFIED PATH IS: MyModule.py
ImportError: No module named MyModule (BY SYSTATE)

This demonstrates that the Jython interpreter nor the sys state were successful at finding the module; provided that the system path was changed specifically so that the folder that the module lives inside is in sys.path.

I've also printed the contents of the folder given to assure that the file does exist.

Thanks in advance for any help!

P.S. I Can guarantee that it is raising an import error as such (if anyone may be suspecting that catching PyException could mean a lot of other things, heres the traceback without a catch block

Exception in thread "main" Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named MyModule
READ ALSO
How good implement method? [closed]

How good implement method? [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

55
Python relative import in subdirectory

Python relative import in subdirectory

I have a Python project, which has something like the following structure:

68
Ability to find certain pages through search

Ability to find certain pages through search

I want to have a search bar that search all the views that are static and supplies search results for example this page here has the function I need: https://cagenixcom/

51