如何使用 Pydev/Eclipse 在 Python 和 Java 项目之间共享通用 Python/Jython 代码?
我正在使用嵌入在 Java 项目中的 Jython 脚本,该脚本是在安装了 Pydev 的 Eclipse 中开发的。现在我想在另一个纯 Python 项目中重用这个脚本。有没有一种方法可以干净地完成此操作而无需复制脚本?
目前,我有一个 Java 项目,其目录为 src/
(包含 Java 源代码)和 src-py/
(包含 Jython 源代码)以及 Pydev 'facet'额外。 Java 代码使用如下内容调用 Jython 代码:
// evaluate the Jython class
PythonInterpreter interpreter = new PythonInterpreter();
InputStream resourceAsStream = MyEclipsePlugin.getResource("/src-py/MyJythonCode.py");
interpreter.execfile(resourceAsStream);
// create an instance of the Jython class and retrieve a Java interface reference to it
interpreter.exec("x = MyJythonClass()"); // MyJythonClass implements MyJavaInterface
Object x = interpreter.get("x").__tojava__(MyJavaInterface.class);
return (MyJavaInterface) x;
这可行,但显然相当难看。从我读到的内容来看,曾经有一个“jythonc”——这可能更接近我需要的——但它似乎已被弃用,并且不鼓励您使用它。如果没有它,我需要在运行时使用实际的 Jython 源代码将其编译为 JVM 类。因此,不幸的是,上述模式似乎不支持加载模块,因此我目前正在单个 .py 文件中处理所有内容。我想我最终可以多次执行 interpreter.execfile()
,但我还没有真正弄清楚当模块之间存在相互依赖性时如何做到这一点。
接下来,我尝试创建一个新的 Jython 项目以将我的 MyJythonCode.py
移入其中。不幸的是,我无法将 Pydev/Jython 项目添加为 Java 项目的项目引用,因此我不确定如何将其集成到 Java 项目中。
我当前看到的选项,我都不喜欢:
- 复制 MyJythonCode.py 并同时修改它们。
- 做一些时髦的 svn:externals 东西,在每次更新时为我进行复制。
- 尝试为 Eclipse 找到一个预构建/自定义构建步骤插件来进行复制。
有人有更好的主意吗?
I'm using a Jython script embedded in a Java project, developed in Eclipse with Pydev installed. Now I'd like to reuse this script in another pure Python project. Is there a way to do this cleanly without copying the script?
Currently, I have a single Java project with the directories src/
(contains the Java sources) and src-py/
(contains the Jython source) and the Pydev 'facet' added. The Java code calls into the Jython code with something like this:
// evaluate the Jython class
PythonInterpreter interpreter = new PythonInterpreter();
InputStream resourceAsStream = MyEclipsePlugin.getResource("/src-py/MyJythonCode.py");
interpreter.execfile(resourceAsStream);
// create an instance of the Jython class and retrieve a Java interface reference to it
interpreter.exec("x = MyJythonClass()"); // MyJythonClass implements MyJavaInterface
Object x = interpreter.get("x").__tojava__(MyJavaInterface.class);
return (MyJavaInterface) x;
This works, but it's obviously quite ugly. From what I read, there was a "jythonc" once -- which is probably closer to what I'd need -- but it seems to have been deprecated and you're discouraged from using it. Without that, I need the actual Jython source code at runtime to compile it into a JVM class. Therefore the above pattern, which unfortunately doesn't seem to support loading modules, so I'm currently working with everything in a single .py file. I guess I could eventually move to executing interpreter.execfile()
multiple times, but I haven't really figured out how to do that when interdependencies between the modules exist.
Next, I tried to create a new Jython project to move my MyJythonCode.py
into. Unfortunately, I can't add Pydev/Jython projects as projects references to Java projects, so I'm not sure how to integrate that into the Java project.
The options I currently see, neither of which I like:
- Copy the MyJythonCode.py and modify them simultaneously.
- Do some funky svn:externals stuff that does the copying for me on every update.
- Try to find a pre-build/custom build step plugin for Eclipse to do the copying.
Anybody have a better idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为正确的做法是为这两个依赖的内容建立一个单独的项目...
即:
将该项目创建为 java 项目,然后向其中添加 pydev 属性(即:右键单击该项目 > pydev > 设置为 pydev 项目)。
然后,在该项目中,您可以创建 java-wrapper 类,并配置 pydev 的源路径(并将该项目添加为其他项目的引用:右键单击其他项目 > 属性 > 项目引用)。
这样,java/python 都应该正常工作。
I think the proper thing would be having a separate project for those contents which both of those depend on...
I.e.:
Create that project as a java project and add the pydev properties to it afterwards (i.e.: right click the project > pydev > set as pydev project).
Then, in that project you can create your java-wrapper classes and also configure the source paths for pydev (and add that project as a reference on the other projects: right click the other projects > properties > project references).
That way, things should work properly for both java/python.