1

Question

Is there a way to reliably ensure that both the python source paths and compiled java class paths are added to the Jython PythonPath when running unit tests under eclipse?

If not, is there a better workaround than adding absolute paths to the PyDev PythonPath, External Libraries list?

Background

I have been having a strange problem with running Jython unit tests on a Jython class which inherits from a Java class.

We have an eclipse RCP application with a structure (simplified) like this:

Config project (Pydev nature only)
    scripts
        src
            classes.py
        test
            classesTest.py

Core project (Both Java & Pydev natures)
    scripts
    src
        baseClass.java
    classes
        baseClass.java

Library project (Both Java & Pydev natues, but no Jython scripts)
    src
        interface.java
    classes
        interface.class

In classes.py I import baseClass, which implements an interface defined in interface.java. I then create a Jython class which inherits from baseClass.

In my classesTest.py, I import classes and define the unit tests.

The problems come when I try to run the unit tests using "Project explorer > classesTest.py > Run as > Jython unit-test":

  1. Sometimes it is unable to find the baseClass (I think it fails with an ImportError, but I can't reproduce this right now).
  2. At other times it finds the baseClass, but then can't find the interface, so it fails with a java.lang.NoClassDefFoundError.

If I pprint the sys.path at the start of the test module, then I can see that:

  1. While core/scripts is on the python path, neither core/classes nor library/classes are on the path.
  2. Both core/scripts and core/classes are on the python path, but library/classes still is not.

Sometimes, very rarely, both classes paths are added to the python path and the unit tests work fine, without the workaround below. Unfortunately, I haven't worked out how to reproduce this and can see no difference in the files in the source tree between it working and it not working.

Current Workaround

My current workaround is to explicitly add in "External Libraries" for library/classes and core/classes explicitly. Unfortunately it appears that these have to be absolute paths, which means I have to set them differently for every trunk, branch or computer that I use. As such, I don't want to commit these into revision control and it is a pain having to set them up every time.

Notes

Note that the Config project only references the core project, but referencing the library project explicitly makes no difference to the class path.

Also, while trying to find a reliable method to reproduce this problem, I had other libraries inexplicably disappear from the python path and then just as mysteriously reappear, the most recent example being Mock!

Update

I haven't seen this problem recently (currently using Eclipse 3.7.1 & PyDev 2.2.2) so it may have been an aberration with the combination of Eclipse/PyDev I was using at the time.

2 Answers 2

1

For a java project, you have to explicitly add the folder with the .class files to the PYTHONPATH.

The recommended organization would be:

java_project/bin <- this folder should be set as a source folder within PyDev (to be added to the PYTHONPATH). java_project/src <- this folder should be added to the jdt classpath, but should not have any reference in PyDev

python_project/src <- this folder should be set as a source folder within PyDev (to be added to the PYTHONPATH).

And the python_project should have a reference to the java_project.

See the last part of http://pydev.org/manual_101_project_conf2.html as a reference.

Sign up to request clarification or add additional context in comments.

Comments

0

I had a similar problem. Recently resolved by accordingly modifying the python.path field in the Jython registry file.

It looks like the Pydev plugin cannot modify the sys.path to be able to import the Python module from a Java project in Eclipse.

More details at Adding in a python module to the sys.path for a Java project in pydev, Eclipse

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.