如何从rJython中导入站点包?

时间:2022-12-11 16:55:12

I am trying to call some functions in openopt package for Python from R using the rJython package. I have the NumPy, SciPy, and OpenOpt installed for Python 2.7.4 on my 64 bit windows 7. Importing openopt from Python is OK.

我正在尝试使用rJython包从R中调用openopt包中的一些函数。我有NumPy, SciPy和OpenOpt在我的64位windows 7上安装了Python 2.7.4。从Python导入openopt是可以的。

However, when I run the codes:

然而,当我运行代码:

library(rJython)
rJython <- rJython(modules='openopt')
rJython$exec("from openopt import GLP")

I got errors:

我得到了错误:

.jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl,  : 
  Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named openopt

Of course I checked the sys.path values for the Python environment:

我当然检查了系统。Python环境的路径值:

rJython$exec("import sys")
rJython$exec("pname = sys.path")
jython.get(rJython, "pname")

but I got:

但我有:

[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" 

These are absolutely not like the values of sys.path in Python itself. So I extended the sys.path by:

这些绝对不像sys的值。路径在Python本身。所以我扩展了系统。路径:

rJython$exec("sys.path.extend(['C:\\Python27\\lib\\site-packages\\openopt-0.45-py2.7.egg'])")

Then it seems that I need to add the numpy to sys.path:

然后,似乎我需要将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

I then extended everything in the sys.path list of Python in rJython. But I still get further information:

然后我扩展了系统中的所有内容。rJython中Python的路径列表。但我仍然得到了更多的信息:

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

OK. The main question is: it seems like I cannot import a module from the site-packages. I can absolutely import basic modules in Python2.7.4. Any suggestion?

好的。主要的问题是:似乎我不能从站点包中导入一个模块。我完全可以导入Python2.7.4中的基本模块。任何建议吗?

1 个解决方案

#1


0  

rJython is a package that does not call Python but Jython, which is not exactly the same thing. In particular, Jython programs cannot currently use CPython extension modules written in C, only those in pure Python.

rJython是一个不调用Python但Jython的包,它不是完全相同的东西。特别是,Jython程序目前不能使用用C编写的CPython扩展模块,只使用纯Python语言的扩展模块。

This is why neither NumPy nor SciPy can be invoked via rJython.

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

However, rPython, a package with exactly the same interface as rJython does interact with the true Python. You should try that.

然而,rPython是一个与rJython完全相同的包,它与真正的Python交互。你应该试试。

There is a caveat, though: it currently only runs on UNIX (Linux/Mac/...) environments, not on Windows. It is possible to have it installed on Windows and there are some instructions as to install it on such platform, but involves some manual configuration of scripts (essentially, to indicate which version of Python is to be used and the path where it is located) and compilation.

不过有一个警告:它目前只运行在UNIX (Linux/Mac/…)环境中,而不是Windows上。在Windows上安装它是可能的,并且在这样的平台上安装它有一些指示,但是涉及到一些脚本的手工配置(从本质上说,是为了指示要使用哪一种Python版本以及它所在的路径)和编译。

#1


0  

rJython is a package that does not call Python but Jython, which is not exactly the same thing. In particular, Jython programs cannot currently use CPython extension modules written in C, only those in pure Python.

rJython是一个不调用Python但Jython的包,它不是完全相同的东西。特别是,Jython程序目前不能使用用C编写的CPython扩展模块,只使用纯Python语言的扩展模块。

This is why neither NumPy nor SciPy can be invoked via rJython.

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

However, rPython, a package with exactly the same interface as rJython does interact with the true Python. You should try that.

然而,rPython是一个与rJython完全相同的包,它与真正的Python交互。你应该试试。

There is a caveat, though: it currently only runs on UNIX (Linux/Mac/...) environments, not on Windows. It is possible to have it installed on Windows and there are some instructions as to install it on such platform, but involves some manual configuration of scripts (essentially, to indicate which version of Python is to be used and the path where it is located) and compilation.

不过有一个警告:它目前只运行在UNIX (Linux/Mac/…)环境中,而不是Windows上。在Windows上安装它是可能的,并且在这样的平台上安装它有一些指示,但是涉及到一些脚本的手工配置(从本质上说,是为了指示要使用哪一种Python版本以及它所在的路径)和编译。