Rpy2 can't find my R libraries on install

2019-04-12 22:33发布

R built from source, installed locally. R is at ~/bin/R (which is in my PATH) and its libraries are in ~/lib64/R/. Installing rpy2 should be simple. It finds the correct R just fine (as it's in the path). Then it can't find the libraries.

$python setup.py build install
R version 3.2.1 (2015-06-18) -- "World-Famous Astronaut"

...

setup.py:211: UserWarning: No include specified
  warnings.warn('No include specified')
setup.py:222: UserWarning: No libraries as -l arguments to the compiler.
  warnings.warn('No libraries as -l arguments to the compiler.')

    Compilation parameters for rpy2's C components:
        include_dirs    = []
        library_dirs    = []
        libraries       = []
        extra_link_args = []

And then we get a million errors that it can't find functions that are in the R libraries.

Rpy2's documentation says there's a simple option for designating where R or its libraries are:

python setup.py build --r-home ~/lib64/R/lib install

But if you do this, then you get:

setup.py:222: UserWarning: No libraries as -l arguments to the compiler.
  warnings.warn('No libraries as -l arguments to the compiler.')

    Compilation parameters for rpy2's C components:
        include_dirs    = []
        library_dirs    = []
        libraries       = []
        extra_link_args = []

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

error: option --r-home not recognized

It looks like the --r-home functionality has been removed. How does one point rpy2 to the correct libraries?


Edit:

Have now installed R with:

./configure --prefix=${HOME} --enable-R-shlib
make
make install

After that, I can install rpy2 with just pip install rpy2. But then, we're still having library problems:

import rpy2.robjects as robjects
ImportError: libRblas.so: cannot open shared object file: No such file or directory

So then I needed to add this to my path:

export LD_LIBRARY_PATH="~/lib64/R/lib:$LD_LIBRARY_PATH"

And then everything works!

标签: python r rpy2
2条回答
Emotional °昔
2楼-- · 2019-04-12 22:53

If R is in an unconventional location, the easiest might be help out a bit by setting environment variables (older versions of the doc is talking about --r-home but this was a less tested corner and it was removed).

Try:

export PATH=~/bin/R:${PATH}
export LD_LIBRARY_PATH=~/lib64/R/lib:${LD_LIBRARY_PATH}
export PKG_CONFIG_PATH=~/lib64/R/lib/pkgconfig/:${PKG_CONFIG_PATH}
查看更多
Viruses.
3楼-- · 2019-04-12 23:12

I've had the best success building rpy2 with non-standard R locations using the following relative path during install:

export LDFLAGS="-Wl,-rpath,~/lib64/R/lib"

Note that you only need to export this in the terminal where you then run python setup.py install and not in your .bashrc or the like. It will store this library path in the rpy2 compiled parts.

This is imho a better option than LD_LIBRARY_PATH which some consider evil:

https://blogs.oracle.com/ali/entry/avoiding_ld_library_path_the

http://linuxmafia.com/faq/Admin/ld-lib-path.html

查看更多
登录 后发表回答