简体   繁体   English

将python模块添加到pydev,Eclipse中的Java项目的sys.path中

[英]Adding in a python module to the sys.path for a Java project in pydev, Eclipse

I've been following the Jython book to be able to get a Java application to import a Python module. 我一直在关注Jython书籍,以便能够获得Java应用程序来导入Python模块。

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" http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html#one-to-one-jython-object-factories明确说“为了使用这种技术来使用Jython模块,你必须确保.py模块包含在sys.path中,或硬编码Java代码中模块的路径“

How do I ensure that the .py module is added to the sys.path within pydev in Eclipse?. 如何确保将.py模块添加到Eclipse中pydev中的sys.path中? I'm using Eclipse Kepler release Build id: 20130614-0229, Pydev version 2.8.1 and JDK 6. 我正在使用Eclipse Kepler发布版本ID:20130614-0229,Pydev版本2.8.1和JDK 6。

I keep getting import errors whenever I try to import a Python module. 每当我尝试导入Python模块时,我都会遇到导入错误。

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 /']. 从Java类打印sys.path,如下面的代码片段所示,sys.path由['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)? 如何在项目属性(或Pydev开发环境中的任何位置)中设置此sys.path?

I do not want to modify sys.path in the Java code? 我不想在Java代码中修改sys.path?

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 ['C:\\ jython2.5.3 \\ Lib',' classpath ',' pyclasspath /']线程“main”中的异常Traceback(最近一次调用last):ImportError中的文件“”,第1行:没有名为Building的模块

UPDATE 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. 根据http://wiki.python.org/jython/JythonFaq/InstallingJython#What_do_.22python.path.22_and_.22python.prepath.22_mean_in_the_Jython_registry.3F ,在Jython注册表文件中修改python.path以将Python模块添加到pythonpath。 Thanks, @SimonC for the hint. 谢谢@SimonC提示。

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. 花了几秒钟看文档(所以我不知道如果这实际上有效),看起来你可以使用PySystemState中的pass到PythonInterpreter构造函数。 The PySystemState has a public path field that (I assume) you can append path entries to. PySystemState有一个公共路径字段(我假设)你可以附加路径条目。

UPDATE 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 ): 如果要在运行时配置中指定路径,那么看起来您可以通过在命令行上指定python.path系统属性(来自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[] {""});

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). 对于PyDev / Eclipse,您应该将文件夹标记为源文件夹(源文件夹是项目中将添加到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). 有关详细信息,请参阅http://pydev.org/manual_101_project_conf2.html (如果您想从jython中指向一个java项目,它最后会有一个部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM