如何让 PythonInterpreter 识别日期时间和其他模块,例如 python 中的 psutils?



我正在运行一个名为 diagnostics.py 的python程序,该程序来自一个名为PyInterpreter的java类,该程序初始化了jython PythonInterpreter对象和要使用的文件,并且可以从该python程序运行方法。我的 python 程序看起来像这样:

import datetime
import psutil
class HeartbeatGenerator:
...

当我尝试运行此程序时,出现错误:

Exception in thread "main" Traceback (most recent call last):
File "../../../eclipse-workspace/Diagnostics/diagnostics.py", line 2, in <module>
import datetime
ImportError: No module named datetime

我已经用pip安装了datetime和psutil,它们位于/usr/local/bin/python3.7/site-packages以及/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages中。我还从该位置复制并粘贴了必要的文件到我的jython.2.5.3/lib/site-packages中,在我的PyInterpreter文件中,我尝试为python.home和python.path设置我的系统属性,如所示:

public PyInterpreter()  
{  
Properties props = System.getProperties();
props.setProperty("python.home", "⁨/usr/local/lib/python3.7⁩⁩");
props.setProperty("python.path", "/usr/local/lib/python3.7/site-packages");
System.out.print(props);
PythonInterpreter.initialize(System.getProperties(),  
props, 
new String[0]);  
this.interpreter = new PythonInterpreter();  
}

但是无论我将 sys 属性设置为什么,我仍然会收到相同的错误。现在它只是在导入日期时间的线上,但我知道如果我先import psutil,它也会在那条线上中断。有什么想法吗?

我想通了。我只需要将 sys.path 上需要的内容添加到我的bash_profile中的 PYTHONPATH 变量中,以便它将其添加到 jython 的 sys.path 变量中。

相关内容

最新更新