Bizzarre试图让Rpy2 2.1.9与R 2.12.1一起使用,在Windows XP下使用Python 2.6 - Rpy无法找到R.dll吗?

时间:2011-02-21 16:52:25

标签: python windows r windows-xp rpy2

我一直在努力让Rpy2在我的R安装中发挥出色。我首先尝试安装rpy2 MSI软件包,但这似乎不起作用。当我运行推荐的测试时,它给了我一个错误,说它无法找到R.dll,因为新的R安装(后2.11)将DLL安装到i386文件夹中,其中rpy2找不到它们,因为它在bin文件夹而不是bin / i386文件夹中查找。

然后我尝试使用python附带的命令行工具(distutils)从头开始构建安装。这不起作用,因为setup.py声称无法找到R_home位置。但我确实编辑了一个环境变量(PATH)可能会显示rpy2设置在哪里找到R安装。然后,我对环境进行了一些编辑,添加了指向bin / i386目录的“R_home”变量,并在PATH变量下创建了一个新条目,指向同一个位置。

不幸的是,当它找到R路径时,我得到了这个问题:

running build
running build_py
running build_ext
Traceback (most recent call last):
  File "setup.py", line 372, in <module>
    [os.path.join('doc', 'source', 'rpy2_logo.png')])]
  File "C:\Python26\lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "C:\Python26\lib\distutils\dist.py", line 975, in run_commands
    self.run_command(cmd)
  File "C:\Python26\lib\distutils\dist.py", line 995, in run_command
    cmd_obj.run()
  File "C:\Python26\lib\distutils\command\build.py", line 134, in run
    self.run_command(cmd_name)
  File "C:\Python26\lib\distutils\cmd.py", line 333, in run_command
    self.distribution.run_command(command)
  File "C:\Python26\lib\distutils\dist.py", line 994, in run_command
    cmd_obj.ensure_finalized()
  File "C:\Python26\lib\distutils\cmd.py", line 117, in ensure_finalized
    self.finalize_options()
  File "setup.py", line 111, in finalize_options
    config += get_rconfig(r_home, about)
  File "setup.py", line 264, in get_rconfig
    rc = RConfig.from_string(rconfig)
  File "setup.py", line 252, in from_string
    + '\nin string\n' + string)
ValueError: Invalid substring in string

所以我回去试图使用premade安装,认为可能对环境的新编辑可能有效但是在这里遇到了这个问题

 Traceback (most recent call last):
      File "<string>", line 245, in run_nodebug
      File "C:\Documents and Settings\User\Desktop\rpy2-2.1.9\rpy\tests.py", line 3, in <module>
        import rpy2.robjects.tests
      File "C:\Python26\lib\site-packages\rpy2\robjects\__init__.py", line 12, in <module>
        import rpy2.rinterface as rinterface
      File "C:\Python26\lib\site-packages\rpy2\rinterface\__init__.py", line 56, in <module>
        raise RuntimeError("Unable to locate R.dll within %s" % R_HOME)
    RuntimeError: Unable to locate R.dll within C:\Program Files\R\R-2.12.1\bin\i386

这真的很奇怪,因为(因为任何人都可以检查他们自己的安装)R将R.dll安装到“C:\ Program Files \ R \ R-2.12.1 \ bin \ i386”并且我已经检查过验证它在那里,我已经将rpy2指向windows默认路径中的这个目录!我知道rpy2看起来正确的地方,但无法理解为什么它没有看到R.dll。

那为什么rpy2找不到呢?有没有人知道让rpy2与R 2.12一起工作的方法?也许我应该尝试更新的rpy2 2.2.0版本?它仍在开发中,1.9应该能够根据这个website处理R 2.12,所以我不知道该怎么办......

感谢任何可以提供帮助的人......

[编辑]我也试过here以上的这些指令,但它们返回相同的“找不到DLL”错误...除非你改变环境变量“R_home”直接指向c / program files / R / R 2.12目录而不是进入i386子目录。

如果它指向正确的位置,则会收到这些错误。这看起来更有希望......但它仍然非常糟糕!

.......................F....................................F.FFF...F....................................................................F..................................
======================================================================
FAIL: testNewWithoutInit (rpy2.rinterface.tests.test_SexpVector.SexpVectorTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_SexpVector.py", line 43, in testNewWithoutInit
    self.assertTrue(False) # worked when tested, but calling endEmbeddedR causes trouble
AssertionError

======================================================================
FAIL: testCallErrorWhenEndedR (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 122, in testCallErrorWhenEndedR
    self.assertTrue(False) # worked when tested, but calling endEmbeddedR causes trouble
AssertionError

======================================================================
FAIL: testReadConsoleWithError (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 117, in testReadConsoleWithError
    self.assertTrue(errorstring.startswith('Traceback'))
AssertionError

======================================================================
FAIL: testSetReadConsole (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 97, in testSetReadConsole
    self.assertEquals(yes.strip(), res[0])
AssertionError: 'yes' != ''

======================================================================
FAIL: testSetWriteConsole (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 36, in testSetWriteConsole
    self.assertEquals('[1] "3"\n', str.join('', buf))
AssertionError: '[1] "3"\n' != ''

======================================================================
FAIL: testWriteConsoleWithError (rpy2.rinterface.tests.test_EmbeddedR.EmbeddedRTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\rinterface\tests\test_EmbeddedR.py", line 55, in testWriteConsoleWithError
    self.assertTrue(errorstring.startswith('Traceback'))
AssertionError

======================================================================
FAIL: testVectorUnicodeCharacter (rpy2.robjects.tests.testNumpyConversions.NumpyConversionsTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\rpy2\robjects\tests\testNumpyConversions.py", line 54, in testVectorUnicodeCharacter
    self.assertTrue(False) # arrays of unicode characters causing segfault
AssertionError

----------------------------------------------------------------------
Ran 172 tests in 0.407s

FAILED (failures=7)
Exit code:  True

3 个答案:

答案 0 :(得分:19)

经过几个小时的网上搜索并尝试了许多不同的事情,其中​​包括遇到与上述相同的问题,我终于让Rpy2在我的Windows 7电脑上工作了!

基本上,关键的帮助来自这个帖子:http://www.mail-archive.com/rpy-list@lists.sourceforge.net/msg03348.html

总结一下,这些是在Windows7上启动并运行rpy2的步骤:

  1. 从此链接安装rpy2:https://bitbucket.org/breisfeld/rpy2_w32_fix/issue/1/binary-installer-for-win32
  2. 将R.dll的路径添加到环境变量PATH(在我的情况下为C:\ Program Files \ R \ R-2.12.1 \ bin \ i386)
  3. 添加环境变量R_HOME(在我的情况下为C:\ Program Files \ R \ R-2.12.1)
  4. 添加环境变量R_USER(只是我在Windows中的用户名)
  5. 重新启动Python IDE(否则不会重新加载环境变量!)

答案 1 :(得分:4)

RPy2未在Windows上测试。您可以尝试在Windows安装程序中使用older version(2.0.8),但是对于较新版本的R可能会遇到问题。

作者不使用Windows。如果您已经掌握了在Windows上使用较新版本的知识,我相信他会欢迎您的贡献。

答案 2 :(得分:2)

我不知道在哪里写这个,因为我无法评论(没有声望点),但觉得这是有用的信息。加剧了“无法找到R.dll ...”错误消息的原因,即使您知道R.dll正好位于它所说的位置,该程序实际上并没有在该目录中查找。我认为相关的操作发生在rpy2的“ init .py”模块中:

import win32api
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'bin', _win_bindir)
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'modules', _win_bindir)
os.environ['PATH'] += ';' + os.path.join(R_HOME, 'lib')

# Load the R dll using the explicit path
R_DLL_DIRS = ('bin', 'lib')
# Try dirs from R_DLL_DIRS
for r_dir in R_DLL_DIRS:
    Rlib = os.path.join(R_HOME, r_dir, _win_bindir, 'R.dll')
    if not os.path.exists(Rlib):
        continue
    win32api.LoadLibrary( Rlib )
    _win_ok = True
    break
# Otherwise fail out!
if not _win_ok:
    raise RuntimeError("Unable to locate R.dll within %s" % R_HOME)

正如您所看到的,错误消息将始终显示它正在查找您设置为R_HOME的任何目录,但它实际上将查找目录“R_HOME \ r_dir \ win_bindir”。因此,当您看到“无法在C:\ Program Files \ R \ R-2.12.1 \ bin \ i386中找到R.dll”这样的消息时,那是因为它实际上是在寻找名为C:\ Program Files \的目录R \ R-2.12.1 \ bin \ i386 \ R-2.12.1 \ bin \ i386 \,或类似的东西。

Kadee的答案中的第3点通过将指定的路径仅保留在\ bin级别的正上方来解决此问题。