Bizzarre issue trying to make Rpy2 2.1.9 work with R 2.12.1, using Python 2.6 under Windows xp - Rpy can't find the R.dll?

荒凉一梦 提交于 2019-12-02 21:09:21

After many hours of searching on the web and trying out many different things, amongst others encountering the same issues as above, I finally got Rpy2 working on my windows 7 computer!

Basically, the crucial help came from this thread: http://www.mail-archive.com/rpy-list@lists.sourceforge.net/msg03348.html.

Summarized, these were the steps to get rpy2 up and running on Windows7:

  1. Install rpy2 from this link: https://bitbucket.org/breisfeld/rpy2_w32_fix/issue/1/binary-installer-for-win32
  2. Add the path to R.dll to the environment variable PATH (C:\Program Files\R\R-2.12.1\bin\i386 in my case)
  3. Add an environment variable R_HOME (C:\Program Files\R\R-2.12.1 in my case)
  4. Add an environment variable R_USER (simply my username in Windows)
  5. Restart your Python IDE (otherwise the environment variables are not reloaded!)

RPy2 isn't tested on Windows. You can try using an older version (2.0.8) with a Windows installer, but that may have trouble with newer versions of R.

The author doesn't use Windows. If you've got the knowledge to get a newer version working on Windows, I'm sure he'd welcome contributions.

I'm not sure where to write this, as I can't comment (no reputation points), but feel it is useful info on this problem. The reason for the aggravating "Unable to locate R.dll within..." error message, even when you know that R.dll is located exactly where it says, is that the program isn't actually looking in that directory. I think the relevant action happens in rpy2's "init.py" module in these lines:

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)

As you can see, the error message will always say that it is looking in whatever directory you have set as R_HOME, but it will actually be looking in the directory "R_HOME\ r_dir\win_bindir". So when you see a message like "Unable to locate R.dll within C:\Program Files\R\R-2.12.1\bin\i386", that's because it is actually looking for a directory named C:\Program Files\R\R-2.12.1\bin\i386\R-2.12.1\bin\i386\, or something like that.

Point 3 in Kadee's answer fixes this by leaving the path specified only down to immediately above the \bin level.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!