我一直在关注 Jython 这本书,以便能够让 Java 应用程序导入 Python 模块。
http://www.jython.org/jythonbook/en/1.0/JythonAndJavaIntegration.html#one-to-one-jython-object-factories具体说“为了利用使用此技术的 Jython 模块,您必须确保 .py 模块包含在您的 sys.path 中,或者在您的 Java 代码中硬编码模块的路径”
如何确保将 .py 模块添加到 Eclipse 中 pydev 中的 sys.path?我正在使用 Eclipse Kepler 版本 Build id:20130614-0229,Pydev 版本 2.8.1 和 JDK 6。
每当我尝试导入 Python 模块时,我总是收到导入错误。
如以下代码片段所示,从 Java 类打印 sys.path 告诉我 sys.path 由 ['C:\jython2.5.3\Lib', 'classpath', ' pyclasspath/']。
如何在项目属性(或 Pydev 开发环境中的任何位置)中设置此 sys.path?
我不想修改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/'] 线程“main”Traceback 中的异常(最近调用最后一次): 文件“”,第 1 行,位于 ImportError: 没有名为 Building 的模块
更新
根据 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。谢谢@SimonC 的提示。
最佳答案
花了几秒钟查看文档(所以我不知道这是否真的有效),看起来你可以在 PySystemState 中使用 pass到 PythonInterpreter构造函数。 PySystemState
有一个公共(public)的 path (我假设)您可以将路径条目附加到的字段。
更新
由于您想在运行时配置中指定路径,因此您可以通过在命令行(来自 Jython FAQ)上指定 python.path
系统属性来实现:
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[] {""});
关于java - 在 pydev、Eclipse 中将 python 模块添加到 Java 项目的 sys.path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18243872/