我正在尝试使用 rJython 包从 R 调用 Python 的 openopt 包中的一些函数。我在我的 64 位 Windows 7 上为 Python 2.7.4 安装了 NumPy、SciPy 和 OpenOpt。从 Python 导入 openopt 是可以的。
但是,当我运行代码时:
library(rJython)
rJython <- rJython(modules='openopt')
rJython$exec("from openopt import GLP")
我收到错误:
.jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named openopt
当然,我检查了 Python 环境的 sys.path 值:
rJython$exec("import sys")
rJython$exec("pname = sys.path")
jython.get(rJython, "pname")
但我得到了:
[1] "C:\\Users\\myname\\Documents\\R\\win-library\\2.15\\rJython\\Lib"
[2] "C:\\Users\\myname\\Documents\\R\\win-library\\2.15\\rJython\\jython.jar\\Lib"
[3] "__classpath__"
[4] "__pyclasspath__/"
[5] "openopt"
[6] "C:/Users/myname/Documents/R/win-library/2.15/rJython"
这些绝对不像 Python 本身中 sys.path 的值。所以我通过以下方式扩展了 sys.path:
rJython$exec("sys.path.extend(['C:\\Python27\\lib\\site-packages\\openopt-0.45-py2.7.egg'])")
然后看来我需要将numpy添加到sys.path:
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python27\lib\site-packages\openopt-0.45-py2.7.egg\openopt\__init__.py", line 10, in <module>
from oo import *
File "C:\Python27\lib\site-packages\openopt-0.45-py2.7.egg\openopt\oo.py", line 4, in <module>
from LP import LP as CLP
File "C:\Python27\lib\site-packages\openopt-0.45-py2.7.egg\openopt\kernel\LP.py", line 1, in <module>
from baseProblem import MatrixProblem
File "C:\Python27\lib\site-packages\openopt-0.45-py2.7.egg\openopt\kernel\baseProblem.py", line 2, in <module>
from numpy import *
ImportError: No module named numpy
然后,我在 rJython 中扩展了 Python 的 sys.path 列表中的所有内容。但我仍然得到更多信息:
Erro in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python27\lib\site-packages\openopt-0.45-py2.7.egg\openopt\__init__.py", line 10, in <module>
from oo import *
File "C:\Python27\lib\site-packages\openopt-0.45-py2.7.egg\openopt\oo.py", line 4, in <module>
from LP import LP as CLP
File "C:\Python27\lib\site-packages\openopt-0.45-py2.7.egg\openopt\kernel\LP.py", line 1, in <module>
from baseProblem import MatrixProblem
File "C:\Python27\lib\site-packages\openopt-0.45-py2.7.egg\openopt\kernel\baseProblem.py", line 2, in <module>
from numpy import *
File "C:\Python27\lib\site-packages\numpy\__init__.py", line 154, in <module>
import add_newdocs
File "C:\Python27\lib\site-packages\numpy\add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "C:\Python27\lib\site-packages\numpy\lib\__init__.py", line 4, in <module>
from type_check import *
File "C:\Python27\lib\site-packages
好的。主要问题是:似乎我无法从站点包中导入模块。我绝对可以在 Python2.7.4 中导入基本模块。有什么建议吗?