2
votes

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?

1
Can your IDE and VCS handle a symlink? - Thomas K
@Thomas That would be the mentioned svn:externals solution then. Every IDE or OS-based solution would mean extra steps when setting up the project on a new developer machine, which I'd really like to avoid. - Henrik Heimbuerger

1 Answers

0
votes

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.