简体   繁体   English

如何从rJython导入站点包?

[英]How to import a site-package from rJython?

I am trying to call some functions in openopt package for Python from R using the rJython package. 我正在尝试使用rJython包从R调用针对Python的openopt包中的某些函数。 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. 我在64位Windows 7上为Python 2.7.4安装了NumPy,SciPy和OpenOpt。从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环境的sys.path值:

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. 这些绝对不像Python本身中的sys.path值。 So I extended the sys.path by: 所以我通过以下方式扩展了sys.path:

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. 然后,我在rJython中扩展了Python的sys.path列表中的所有内容。 But I still get further information: 但是我仍然获得更多信息:

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. 我绝对可以在Python2.7.4中导入基本模块。 Any suggestion? 有什么建议吗?

rJython is a package that does not call Python but Jython , which is not exactly the same thing. rJython是一个不调用Python而是Jython的软件包,这不是一回事。 In particular, Jython programs cannot currently use CPython extension modules written in C , only those in pure Python. 特别是, 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. 然而, rPython ,用完全相同的接口rJython确实与真正的Python交互的包。 You should try that. 您应该尝试一下。

There is a caveat, though: it currently only runs on UNIX (Linux/Mac/...) environments, not on Windows. 需要注意的是:它目前仅在UNIX(Linux / Mac / ...)环境下运行,而在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. 可以将其安装在Windows上,并且有一些在该平台上安装的说明,但是涉及一些脚本的手动配置(本质上是指示要使用的Python版本以及其所在路径)和编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM