如何从rJython导入site-package?

时间:2013-05-17 06:21:40

标签: python r numpy import scipy

我正在尝试使用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

行。主要问题是:好像我无法从site-packages导入模块。我绝对可以在Python2.7.4中导入基本模块。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

rJython是一个不调用Python而不是Jython的包,这不是完全相同的东西。特别是Jython programs cannot currently use CPython extension modules written in C,只有纯Python中的那些。

这就是为什么NumPy和SciPy都不能通过rJython调用。

但是,rPython,与rJython具有完全相同接口的包与 true Python进行交互。你应该试试。

但有一点需要注意:它目前只在UNIX(Linux / Mac / ...)环境中运行,而不是在Windows上运行。可以在Windows上安装它,并且有一些说明可以在这样的平台上安装它,但是需要一些脚本的手动配置(本质上,用于指示要使用哪个版本的Python以及它所在的路径)和编译。