通过 Jython 加载 Python class
Load Python class through Jython
我正在尝试通过在我的 Java 应用程序中嵌入 Jython 来加载 Python class。
我目前的代码是
String pythonRoot = Main.class.getResource("/python").getPath();
PySystemState state = new PySystemState();
PyObject importer = state.getBuiltins().__getitem__(Py.newString("__import__"));
PyObject sysModule = importer.__call__(Py.newString("sys"));
final PyString pythonPath = Py.newString(pythonRoot);
PyList path = (PyList) sysModule.__getattr__("path");
path.add(pythonPath);
PyModule module = (PyModule) importer.__call__(Py.newString("building.blah.again.blah2.Test"));
PyObject klass = module.__getattr__(Py.newString("Address"));
AddressInterface ai = (AddressInterface) klass.__call__().__tojava__(AddressInterface.class);
我正在尝试访问的class可以在
中找到
/python/building/blah/again/blah2/Test.py
class 的名字是
Address
然而,这给了我例外
Exception in thread "main" ImportError: No module named blah2
如果我在上面的目录中放置一些文件,就像这样
/python/building/blah/again/Test.py
这给了我例外
Exception in thread "main" ImportError: No module named again
就好像他在主动拒绝识别包含文件的目录。这可能是什么问题,我该如何解决这个问题?
如果您通过 path.add(pythonPath);
将模块的路径添加到 Python 路径,导入命令只需要模块的名称,而不是完整路径,即 PyModule module = (PyModule) importer.__call__(Py.newString("Test"));
此外,您应该通过打印路径列表的内容来确认实际上添加了正确的路径。另请注意,您在 Test.py 中的 class-声明必须扩展 Address-java-interface 才能使 toJava
工作(我之所以提到这一点,是因为这也是错误)。
也就是说,您使用 Jython 的方式对我来说似乎有些麻烦。如果我是你,我会在 python-脚本中执行这些操作(添加路径,执行导入),运行 通过 org.python.util.PythonInterpreter
(http://www.jython.org/javadoc/org/python/util/PythonInterpreter.html) 并检索class-PyObject 通过 eval
或 get
-方法。
我正在尝试通过在我的 Java 应用程序中嵌入 Jython 来加载 Python class。
我目前的代码是
String pythonRoot = Main.class.getResource("/python").getPath(); PySystemState state = new PySystemState(); PyObject importer = state.getBuiltins().__getitem__(Py.newString("__import__")); PyObject sysModule = importer.__call__(Py.newString("sys")); final PyString pythonPath = Py.newString(pythonRoot); PyList path = (PyList) sysModule.__getattr__("path"); path.add(pythonPath); PyModule module = (PyModule) importer.__call__(Py.newString("building.blah.again.blah2.Test")); PyObject klass = module.__getattr__(Py.newString("Address")); AddressInterface ai = (AddressInterface) klass.__call__().__tojava__(AddressInterface.class);
我正在尝试访问的class可以在
中找到/python/building/blah/again/blah2/Test.py
class 的名字是
Address
然而,这给了我例外
Exception in thread "main" ImportError: No module named blah2
如果我在上面的目录中放置一些文件,就像这样
/python/building/blah/again/Test.py
这给了我例外
Exception in thread "main" ImportError: No module named again
就好像他在主动拒绝识别包含文件的目录。这可能是什么问题,我该如何解决这个问题?
如果您通过 path.add(pythonPath);
将模块的路径添加到 Python 路径,导入命令只需要模块的名称,而不是完整路径,即 PyModule module = (PyModule) importer.__call__(Py.newString("Test"));
此外,您应该通过打印路径列表的内容来确认实际上添加了正确的路径。另请注意,您在 Test.py 中的 class-声明必须扩展 Address-java-interface 才能使 toJava
工作(我之所以提到这一点,是因为这也是错误)。
也就是说,您使用 Jython 的方式对我来说似乎有些麻烦。如果我是你,我会在 python-脚本中执行这些操作(添加路径,执行导入),运行 通过 org.python.util.PythonInterpreter
(http://www.jython.org/javadoc/org/python/util/PythonInterpreter.html) 并检索class-PyObject 通过 eval
或 get
-方法。