如何让 Jython PythonPath 可靠地包含 java 类路径?

发布于 2024-09-30 19:32:46 字数 2075 浏览 0 评论 0原文

问题

有没有一种方法可以可靠地确保在 eclipse 下运行单元测试时将 python 源路径和编译后的 java 类路径添加到 Jython PythonPath 中?

如果没有,是否有比将绝对路径添加到 PyDev PythonPath、外部库列表更好的解决方法?

背景

在继承自 Java 类的 Jython 类上运行 Jython 单元测试时,我遇到了一个奇怪的问题。

我们有一个 Eclipse RCP 应用程序,其结构(简化)如下:

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

classes.py 中,我导入 baseClass,它实现了定义在中的 interface interface.java。然后,我创建一个继承自 baseClass 的 Jython 类。

在我的 classesTest.py 中,我导入类并定义单元测试。

当我尝试使用“Project explorer>classesTest.py>Run as>Jythonunit-test”运行单元测试时,问题就出现了:

  1. 有时它无法找到baseClass(我认为它失败并出现 ImportError,但我现在无法重现这一点)。
  2. 有时它会找到baseClass,但随后找不到interface,因此会失败并出现java.lang.NoClassDefFoundError

如果我在测试模块的开头 pprint sys.path ,那么我可以看到:

  1. 虽然 core/scripts 位于 python 上路径,core/classeslibrary/classes 都不在路径上。
  2. core/scriptscore/classes 都在 python 路径上,但 library/classes 仍然不在。

有时(非常罕见),两个类路径都会添加到 python 路径中,并且单元测试可以正常工作,而无需使用下面的解决方法。不幸的是,我还没有弄清楚如何重现这一点,并且可以看到源树中的文件在工作和不工作之间没有任何区别。

当前的解决方法

我当前的解决方法是显式添加 library/classescore/classes 的“外部库”。不幸的是,这些路径似乎必须是绝对路径,这意味着我必须为我使用的每个主干、分支或计算机设置不同的路径。因此,我不想将它们提交到版本控制中,并且每次都必须设置它们是一种痛苦。

注意

请注意,Config 项目仅引用核心项目,但显式引用库项目对类路径没有影响。

另外,在试图找到一种可靠的方法来重现这个问题时,我让其他库莫名其妙地从 python 路径中消失,然后又神秘地重新出现,最近的例子是 Mock!

更新

我最近没有看到这个问题(当前使用 Eclipse 3.7.1 和 PyDev 2.2.2),所以它可能是我当时使用的 Eclipse/PyDev 组合的一个异常。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜夜流光相皎洁 2024-10-07 19:32:46

对于 java 项目,您必须将包含 .class 文件的文件夹显式添加到 PYTHONPATH。

推荐的组织是:

java_project/bin <- 该文件夹应设置为 PyDev 中的源文件夹(将添加到 PYTHONPATH)。
java_project/src <- 此文件夹应添加到 jdt 类路径中,但在 PyDev 中不应有任何引用

python_project/src <- 此文件夹应设置为 PyDev 中的源文件夹(要添加到 PYTHONPATH)。

并且 python_project 应该有对 java_project 的引用。

请参阅 http://pydev.org/manual_101_project_conf2.html 的最后部分作为参考。

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.

相思故 2024-10-07 19:32:46

我有类似的问题。最近通过相应修改 Jython 注册表文件中的 python.path 字段解决了这个问题。

看起来 Pydev 插件无法修改 sys.path 以便能够从 Eclipse 中的 Java 项目导入 Python 模块。

更多详细信息请参见 将 python 模块添加到 pydev、Eclipse 中 Java 项目的 sys.path

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文