简体   繁体   中英

How do I share common Python/Jython code between a Python and Java project with Pydev/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?

I think the proper thing would be having a separate project for those contents which both of those depend on...

Ie:

Create that project as a java project and add the pydev properties to it afterwards (ie: 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.

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