简体   繁体   English

从Java调用jython方法:在导入中找不到模块

[英]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. 问题陈述:我正在使用PySystemState来调用java类中的jython模块并对其执行一些方法。 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. 现在的问题是,我的jython模块与位于同一jython项目中并行目录中的其他一些jython模块具有某些依赖性。 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: 我的jython项目的结构:

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 异常=>没有名为folder2的模块

Please note that in java project I'm setting up JYTHONPATH env variable with path to both folder1 and folder2. 请注意,在Java项目中,我正在设置JYTHONPATH env变量,该变量的路径分别为folder1和folder2。

I'm using eclipse environment with jython 5.3. 我在yython 5.3中使用eclipse环境。

2) some more research : I'm using PySystemState to invoke jython modules from java. 2)更多研究:我正在使用PySystemState从Java调用jython模块。 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. 现在看来,我只导入Folder1中的模块,然后在其中获取类以调用方法。 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. 现在,由于此模块已从位于folder2中的其他模块导入,并且未以Java代码加载,因此无法找到该模块。 Now the question is how should I load dependent modules in folder2 along with the module in folder1 which I need. 现在的问题是,我应该如何在我需要的文件夹2中加载依赖的模块以及在文件夹1中的模块。

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. 伙计们,我注意到文件夹2中的另一个模块(该模块是从Java调用的模块中导入的)中没有类。 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. 因此,我从Java调用的模块有一个我正在调用的类,并且该类依赖于jython模块,该模块具有一些方法,但不是类。 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. 尝试了其他一些事情,结果是这样的:场景1:pkg1的class1为class1 pkg2的class2为module2,模块1的import => from module2 import class2现在,当我从Java程序调用class1时,它可以正常工作。

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. 方案2:pkg1的模块1的类为pkg2的模块2的方法定义且没有类,并且模块1的import => from pkg2 import module2现在,当我从Java程序调用class1时,这引发了异常。

Conclusions: 1) we can't use imports from package when invoking a jython class from java. 结论:1)从Java调用jython类时,不能使用从包中导入。 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). 2)我们不能从java中调用一个jython类,它是从一个没有类的模块(只是方法定义)中导入的。 Because jython will only allow "from pkg import " in this scenario and again we'll have problem as in point 1. 因为在这种情况下jython仅允许“从pkg导入”,因此再次遇到第1点中的问题。

You probably need to set JYTHONPATH. 您可能需要设置JYTHONPATH。 See related Stackoverflow question here. 在这里查看相关的Stackoverflow问题。 Jython does not load PYTHONPATH into sys.path Jython不会将PYTHONPATH加载到sys.path中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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