Question

I've been following the Jython book to be able to get a Java application to import a Python module.

http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html#one-to-one-jython-object-factories specifically says "In order to utilize a Jython module using this technique, you must either ensure that the .py module is contained within your sys.path, or hard code the path to the module within your Java code"

How do I ensure that the .py module is added to the sys.path within pydev in Eclipse?. I'm using Eclipse Kepler release Build id: 20130614-0229, Pydev version 2.8.1 and JDK 6.

I keep getting import errors whenever I try to import a Python module.

Printing sys.path from the Java class as in the following snippet tells me that the sys.path is composed of ['C:\jython2.5.3\Lib', 'classpath', 'pyclasspath/'].

How do I set this sys.path in the project properties (or anywhere within the Pydev development environment)?

I do not want to modify sys.path in the Java code?

public BuildingFactory() {
    PythonInterpreter interpreter = new PythonInterpreter();
    interpreter.exec("import sys");
    interpreter.exec("import sys.path");
    interpreter.exec("print sys.path");
    interpreter.exec("from Building import Building");
    buildingClass = interpreter.get("Building");
    }

['C:\jython2.5.3\Lib', 'classpath', 'pyclasspath/'] Exception in thread "main" Traceback (most recent call last): File "", line 1, in ImportError: No module named Building

UPDATE

Per http://wiki.python.org/jython/JythonFaq/InstallingJython#What_do_.22python.path.22_and_.22python.prepath.22_mean_in_the_Jython_registry.3F, the python.path was modified in the Jython registry file to add the Python module to the pythonpath. Thanks, @SimonC for the hint.

Was it helpful?

Solution

Having spent a few seconds looking at the documentation (so I've no ide if this actually works), it looks like you can use pass in a PySystemState to the PythonInterpreter constructor. The PySystemState has a public path field that (I assume) you can append path entries to.

UPDATE

As you want to specify the path in the runtime configuration, then it looks like you can do this by specifying the python.path system property on the command line (from the Jython FAQ):

Properties props = new Properties();
// set in the VM args in the Eclipse runtime configuration instead
// props.setProperty("python.path", "/home/modules:scripts");
PythonInterpreter.initialize(System.getProperties(), props,
                             new String[] {""});

OTHER TIPS

For PyDev/Eclipse, you should mark your folder as a source folder (source folders are the folders in the project which will be added to the PYTHONPATH).

See http://pydev.org/manual_101_project_conf2.html for details (and it has a section in the end if you want to point to a java project from within jython).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top