如何使用 Eclipse 导入 Python 类?

发布于 2024-09-02 11:59:15 字数 743 浏览 2 评论 0原文

我正在尝试编写 Jython,其中 Python 文件从 Java 导入类 我正在使用 Eclipse 和 PyDev。

我的 Python 代码如下所示:

from eclipsejavatest import eclipseJavaTest
from eclipsejavatest import JavaClass

class eclipsePyPrint(eclipseJavaTest):
    def eclipsepyMain(self):
        print "python main method"
        eclipseJavaTest.printerCount(4)
        print eclipseJavaTest.gotoPython()
        eclipseJavaTest.printerSentence()

        samplepyClass = JavaClass("Jython plain")
        samplepyClass.setName("jython fancy")
        print samplepyClass.getName()

但我收到错误 ImportError: No module named eclipsejavatest

Python 项目引用了 Java 项目。我尝试导出 Java 项目并将 .jar 添加到 Python 项目的 Jython 类路径中。

我不知道该怎么做才能让它发挥作用。

I'm trying to write Jython where the Python file imports classes from Java
I'm using Eclipse with PyDev.

My Python code looks like:

from eclipsejavatest import eclipseJavaTest
from eclipsejavatest import JavaClass

class eclipsePyPrint(eclipseJavaTest):
    def eclipsepyMain(self):
        print "python main method"
        eclipseJavaTest.printerCount(4)
        print eclipseJavaTest.gotoPython()
        eclipseJavaTest.printerSentence()

        samplepyClass = JavaClass("Jython plain")
        samplepyClass.setName("jython fancy")
        print samplepyClass.getName()

but I'm getting the error ImportError: No module named eclipsejavatest

The Python project references the Java project. I've tried exporting the Java project and adding the .jar to the Jython Class Path for the Python project.

I'm not sure what to do to get this to work.

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

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

发布评论

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

评论(3

偏爱你一生 2024-09-09 11:59:15

我还将 Eclipse 与 Pydev 一起使用。我想从 Jython 项目文件夹内的程序调用工作区中 Java 项目的 Java 类。我不必右键单击 java 包并将其设置为 pydev 项目。在我的设置中,当我尝试将 java 源文件夹添加到 PYTHONPATH 时,无论我是否将其配置为 Pydev 文件夹,它都不会显示。我必须将其添加为外部库。

我右键单击 Jython 项目,选择属性,选择 PyDev - PYTHONPATH,选择外部库选项卡,然后选择添加源文件夹按钮。我添加了包含 java 项目中的 .class 文件的文件夹; C:\blah blah blah\workspace\javafoldername\bin。起初,我尝试使用 ...\javafoldername\src 添加 .java 文件,但这不起作用。

I am also using Eclipse with Pydev. I wanted to call Java classes from a Java project in my workspace from a program inside a Jython project folder. I did NOT have to right click on the java package and set it as a pydev project. In my setup the java source folder would not show up when I tried to add it to the PYTHONPATH regardless of whether I configured it as a Pydev folder. I had to add it as an external library.

I right-clicked on the Jython project, selected properties, selected PyDev - PYTHONPATH, selected the External Libraries tab, and then selected the add source folder button. I added the folder with the .class files from the java project; C:\blah blah blah\workspace\javafoldername\bin. At first I tried to use ...\javafoldername\src to add the .java files but that didn't work.

荒芜了季节 2024-09-09 11:59:15

想通了——
我必须右键单击 java 包并将其设置为 pydev 项目
然后我必须进入 python 项目并添加 .class 文件

Figured it out -
I had to right click the java package and set it as a pydev project
Then I had to go into the python project and add the .class files

风为裳 2024-09-09 11:59:15

尝试 :

import eclipsejavatest.eclipseJavaTest as eclipseJavaTest

Try :

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