简体   繁体   中英

invoking jython methods from java : couldn't locate modules in import

Problem statement: I'm using PySystemState to invoke my jython module in java class and execute some methods on it. Now the problem is that my jython module has some dependencies on some other jython modules located in paralled directories in the same jython project. And as a result when I invoke the target module it throws import exception saying dependent module can't be located.

Structure of my jython project:

jythonproject
  -src
     -folder1<has the module which is invoked from java>
     -folder2 < it has the modules which the module inside folder1 imports with statemet "from folder2 import x"

Exception => No module named folder2

Please note that in java project I'm setting up JYTHONPATH env variable with path to both folder1 and folder2.

I'm using eclipse environment with jython 5.3.

2) some more research : I'm using PySystemState to invoke jython modules from java. Here is the spec to load the module and class.

 // Constructor obtains a reference to the importer, module, and the class name
 public JythonObjectFactory(PySystemState state, Class interfaceType, String moduleName, String className) {
     this.interfaceType = interfaceType;
     PyObject importer = state.getBuiltins().__getitem__(Py.newString("__import__"));
     PyObject module = importer.__call__(Py.newString(moduleName));
     klass = module.__getattr__(className);
     System.err.println("module=" + module + ",class=" + klass);
 }

NOw it seems that I'm importing only the module in folder1 and then getting the class inside it to invoke methods. Now since this module has imports from other modules which are located in folder2 and they are not loaded in java code hence it fails to located the module. Now the question is how should I load dependent modules in folder2 along with the module in folder1 which I need.

Guys I noticed one more thing the module in folder 2 which is imported in module which I'm invoking from java doesn't have a class in it. So module which I'm invoking from java has a class which I'm invoking and that class has dependency on a jython module which has some methods but it's not a class. By any chance is this the reason why it's not working and I'm getting an exception.

Tried few other things and here is the outcome: Scenario 1: pkg1 has module1 with class1 pkg2 has module2 with class2 And module 1 has an import => from module2 import class2 Now when I invoke class1 from java program this works fine.

Scenario2: pkg1 has module1 with class1 pkg2 has module2 which has method definitions and no class And module 1 has an import => from pkg2 import module2 Now when I invoke class1 from java program this throws me exception.

Conclusions: 1) we can't use imports from package when invoking a jython class from java. 2) we can't invoke a jython class from java which has imports from a module which doesn't have a class(just method definitions). Because jython will only allow "from pkg import " in this scenario and again we'll have problem as in point 1.

You probably need to set JYTHONPATH. See related Stackoverflow question here. Jython does not load PYTHONPATH into sys.path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM