conda-forge / rpy2-feedstock

A conda-smithy repository for rpy2.
BSD 3-Clause "New" or "Revised" License
3 stars 19 forks source link

jupyter import error #112

Open hschult opened 1 year ago

hschult commented 1 year ago

Solution to issue cannot be found in the documentation.

Issue

Basically the problem reported here: https://github.com/rpy2/rpy2/issues/858

I created an environment with: mamba create -n rpy2 rpy2 ipykernel anndata2ri registered it as a kernel and tried to import rpy2.robjects which gave me the following error:

import rpy2.robjects

Unable to determine R home: [Errno 2] No such file or directory: 'R'

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[2], line 1
----> 1 import rpy2.robjects

File /workspace/.conda/rpy2/lib/python3.11/site-packages/rpy2/robjects/__init__.py:18
     15 import rpy2.rinterface as rinterface
     16 import rpy2.rlike.container as rlc
---> 18 from rpy2.robjects.robject import RObjectMixin, RObject
     19 import rpy2.robjects.functions
     20 from rpy2.robjects.environments import (Environment,
     21                                         local_context)

File /workspace/.conda/rpy2/lib/python3.11/site-packages/rpy2/robjects/robject.py:11
      7 import rpy2.rinterface_lib.callbacks
      9 from rpy2.robjects import conversion
---> 11 rpy2.rinterface.initr_simple()
     14 def _add_warn_reticulate_hook():
     15     msg = """
     16     WARNING: The R package "reticulate" only fixed recently
     17     an issue that caused a segfault when used with rpy2:
   (...)
     20     the fix.
     21     """

File /workspace/.conda/rpy2/lib/python3.11/site-packages/rpy2/rinterface.py:998, in initr_simple()
    996 """Initialize R's embedded C library."""
    997 with openrlib.rlock:
--> 998     status = embedded._initr()
    999     atexit.register(endr, 0)
   1000     _rinterface._register_external_symbols()

File /workspace/.conda/rpy2/lib/python3.11/site-packages/rpy2/rinterface_lib/embedded.py:272, in _initr(interactive, _want_setcallbacks, _c_stack_limit)
    270     return None
    271 elif openrlib.R_HOME is None:
--> 272     raise ValueError('openrlib.R_HOME cannot be None.')
    273 elif openrlib.rlib.R_NilValue != ffi.NULL:
    274     msg = ('R was initialized outside of rpy2 (R_NilValue != NULL). '
    275            'Trying to use it nevertheless.')

ValueError: openrlib.R_HOME cannot be None.

Interestingly if I open python in the terminal and do the same import it works just fine. Also since the error states that the path to r_home is missing I tried to set it.

import os
os.environ["R_HOME"] = "/workspace/.conda/rpy2/lib/R"

import rpy2.robjects

When the above is done directly after restarting the kernel it works just fine. I guess there might be an additional path variable in jupyter that needs to be set during installation?

Installed packages

# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
_r-mutex                  1.0.1               anacondar_1    conda-forge
anndata                   0.9.1              pyhd8ed1ab_0    conda-forge
anndata2ri                1.1                pyhdfd78af_0    bioconda
asttokens                 2.2.1              pyhd8ed1ab_0    conda-forge
backcall                  0.2.0              pyh9f0ad1d_0    conda-forge
backports                 1.1                pyhd3eb1b0_0
backports.functools_lru_cache 1.6.4              pyhd8ed1ab_0    conda-forge
binutils_impl_linux-64    2.40                 hf600244_0    conda-forge
brotli                    1.0.9                h166bdaf_8    conda-forge
brotli-bin                1.0.9                h166bdaf_8    conda-forge
bwidget                   1.9.14               ha770c72_1    conda-forge
bzip2                     1.0.8                h7f98852_4    conda-forge
c-ares                    1.19.1               hd590300_0    conda-forge
ca-certificates           2023.05.30           h06a4308_0
cached-property           1.5.2                hd8ed1ab_1    conda-forge
cached_property           1.5.2              pyha770c72_1    conda-forge
cairo                     1.16.0            hbbf8b49_1016    conda-forge
certifi                   2023.5.7           pyhd8ed1ab_0    conda-forge
cffi                      1.15.1          py311h409f033_3    conda-forge
charset-normalizer        3.1.0              pyhd8ed1ab_0    conda-forge
comm                      0.1.3              pyhd8ed1ab_0    conda-forge
curl                      8.1.2                h409715c_0    conda-forge
debugpy                   1.6.7           py311hcafe171_0    conda-forge
decorator                 5.1.1              pyhd8ed1ab_0    conda-forge
dunamai                   1.17.0             pyhd8ed1ab_0    conda-forge
executing                 1.2.0              pyhd8ed1ab_0    conda-forge
expat                     2.5.0                hcb278e6_1    conda-forge
font-ttf-dejavu-sans-mono 2.37                 hab24e00_0    conda-forge
font-ttf-inconsolata      3.000                h77eed37_0    conda-forge
font-ttf-source-code-pro  2.038                h77eed37_0    conda-forge
font-ttf-ubuntu           0.83                 hab24e00_0    conda-forge
fontconfig                2.14.2               h14ed4e7_0    conda-forge
fonts-conda-ecosystem     1                             0    conda-forge
fonts-conda-forge         1                             0    conda-forge
freetype                  2.12.1               hca18f0e_1    conda-forge
fribidi                   1.0.10               h36c2ea0_0    conda-forge
gcc_impl_linux-64         13.1.0               hc4be1a9_0    conda-forge
gdbm                      1.18                 h0a1914f_2    conda-forge
get_version               3.5.4              pyhd8ed1ab_0    conda-forge
gettext                   0.21.1               h27087fc_0    conda-forge
gfortran_impl_linux-64    13.1.0               hd511a9b_0    conda-forge
git                       2.41.0          pl5321h86e50cf_0    conda-forge
graphite2                 1.3.14               h295c915_1
gsl                       2.7.1                h6e86dc7_1
gxx_impl_linux-64         13.1.0               hc4be1a9_0    conda-forge
h5py                      3.9.0           nompi_py311h26d1283_100    conda-forge
harfbuzz                  7.3.0                hdb3a94d_0    conda-forge
hdf5                      1.14.0          nompi_hb72d44e_103    conda-forge
icu                       72.1                 hcb278e6_0    conda-forge
idna                      3.4                pyhd8ed1ab_0    conda-forge
importlib-metadata        6.7.0              pyha770c72_0    conda-forge
importlib_metadata        6.7.0                hd8ed1ab_0    conda-forge
ipykernel                 6.23.2             pyh210e3f2_0    conda-forge
ipython                   8.14.0             pyh41d4057_0    conda-forge
jedi                      0.18.2             pyhd8ed1ab_0    conda-forge
jinja2                    3.1.2              pyhd8ed1ab_1    conda-forge
jupyter_client            8.2.0              pyhd8ed1ab_0    conda-forge
jupyter_core              5.3.1           py311h38be061_0    conda-forge
kernel-headers_linux-64   2.6.32              he073ed8_15    conda-forge
keyutils                  1.6.1                h166bdaf_0    conda-forge
krb5                      1.20.1               h81ceb04_0    conda-forge
ld_impl_linux-64          2.40                 h41732ed_0    conda-forge
lerc                      4.0.0                h27087fc_0    conda-forge
libaec                    1.0.6                hcb278e6_1    conda-forge
libblas                   3.9.0           17_linux64_openblas    conda-forge
libbrotlicommon           1.0.9                h166bdaf_8    conda-forge
libbrotlidec              1.0.9                h166bdaf_8    conda-forge
libbrotlienc              1.0.9                h166bdaf_8    conda-forge
libcblas                  3.9.0           17_linux64_openblas    conda-forge
libcurl                   8.1.2                h409715c_0    conda-forge
libdeflate                1.18                 h0b41bf4_0    conda-forge
libedit                   3.1.20221030         h5eee18b_0
libev                     4.33                 h516909a_1    conda-forge
libexpat                  2.5.0                hcb278e6_1    conda-forge
libffi                    3.4.4                h6a678d5_0
libgcc-devel_linux-64     13.1.0               he3cc6c4_0    conda-forge
libgcc-ng                 13.1.0               he5830b7_0    conda-forge
libgfortran-ng            13.1.0               h69a702a_0    conda-forge
libgfortran5              13.1.0               h15d22d2_0    conda-forge
libglib                   2.76.3               hebfc3b9_0    conda-forge
libgomp                   13.1.0               he5830b7_0    conda-forge
libiconv                  1.17                 h166bdaf_0    conda-forge
libjpeg-turbo             2.1.5.1              h0b41bf4_0    conda-forge
liblapack                 3.9.0           17_linux64_openblas    conda-forge
libnghttp2                1.52.0               h61bc06f_0    conda-forge
libnsl                    2.0.0                h7f98852_0    conda-forge
libopenblas               0.3.23          pthreads_h80387f5_0    conda-forge
libpng                    1.6.39               h753d276_0    conda-forge
libsanitizer              13.1.0               hfd8a6a1_0    conda-forge
libsodium                 1.0.18               h36c2ea0_1    conda-forge
libsqlite                 3.42.0               h2797004_0    conda-forge
libssh2                   1.11.0               h0841786_0    conda-forge
libstdcxx-devel_linux-64  13.1.0               he3cc6c4_0    conda-forge
libstdcxx-ng              13.1.0               hfd8a6a1_0    conda-forge
libtiff                   4.5.1                h8b53f26_0    conda-forge
libuuid                   2.38.1               h0b41bf4_0    conda-forge
libwebp-base              1.3.0                h0b41bf4_0    conda-forge
libxcb                    1.15                 h0b41bf4_0    conda-forge
libxml2                   2.11.4               h0d562d8_0    conda-forge
libzlib                   1.2.13               hd590300_5    conda-forge
lz4-c                     1.9.4                hcb278e6_0    conda-forge
make                      4.3                  hd18ef5c_1    conda-forge
markupsafe                2.1.3           py311h459d7ec_0    conda-forge
matplotlib-inline         0.1.6              pyhd8ed1ab_0    conda-forge
natsort                   8.4.0              pyhd8ed1ab_0    conda-forge
ncurses                   6.4                  hcb278e6_0    conda-forge
nest-asyncio              1.5.6              pyhd8ed1ab_0    conda-forge
numpy                     1.25.0          py311h64a7726_0    conda-forge
openssl                   3.1.1                hd590300_1    conda-forge
packaging                 23.1               pyhd8ed1ab_0    conda-forge
pandas                    2.0.2           py311h320fe9a_0    conda-forge
pango                     1.50.14              heaa33ce_1    conda-forge
parso                     0.8.3              pyhd8ed1ab_0    conda-forge
pcre2                     10.40                hc3806b6_0    conda-forge
perl                      5.34.0               h5eee18b_2
pexpect                   4.8.0              pyh1a96a4e_2    conda-forge
pickleshare               0.7.5                   py_1003    conda-forge
pip                       23.1.2             pyhd8ed1ab_0    conda-forge
pixman                    0.40.0               h36c2ea0_0    conda-forge
platformdirs              3.6.0              pyhd8ed1ab_0    conda-forge
pooch                     1.7.0              pyha770c72_3    conda-forge
prompt-toolkit            3.0.38             pyha770c72_0    conda-forge
prompt_toolkit            3.0.38               hd8ed1ab_0    conda-forge
psutil                    5.9.5           py311h2582759_0    conda-forge
pthread-stubs             0.4               h36c2ea0_1001    conda-forge
ptyprocess                0.7.0              pyhd3deb0d_0    conda-forge
pure_eval                 0.2.2              pyhd8ed1ab_0    conda-forge
pycparser                 2.21               pyhd8ed1ab_0    conda-forge
pygments                  2.15.1             pyhd8ed1ab_0    conda-forge
pysocks                   1.7.1              pyha2e5f31_6    conda-forge
python                    3.11.4          hab00c5b_0_cpython    conda-forge
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python-tzdata             2023.3             pyhd8ed1ab_0    conda-forge
python_abi                3.11                    3_cp311    conda-forge
pytz                      2023.3             pyhd8ed1ab_0    conda-forge
pyzmq                     25.1.0          py311h75c88c4_0    conda-forge
r-base                    4.2.3                hfabd6f2_3    conda-forge
readline                  8.2                  h8228510_1    conda-forge
requests                  2.31.0             pyhd8ed1ab_0    conda-forge
rpy2                      3.5.11          py311r42hcb2cf0a_0    conda-forge
scipy                     1.10.1          py311h64a7726_3    conda-forge
sed                       4.8                  he412f7d_0    conda-forge
setuptools                68.0.0             pyhd8ed1ab_0    conda-forge
simplegeneric             0.8.1                      py_1    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
stack_data                0.6.2              pyhd8ed1ab_0    conda-forge
sysroot_linux-64          2.12                he073ed8_15    conda-forge
tk                        8.6.12               h27826a3_0    conda-forge
tktable                   2.10                 hb7b940f_3    conda-forge
tornado                   6.3.2           py311h459d7ec_0    conda-forge
traitlets                 5.9.0              pyhd8ed1ab_0    conda-forge
typing-extensions         4.6.3                hd8ed1ab_0    conda-forge
typing_extensions         4.6.3              pyha770c72_0    conda-forge
tzdata                    2023c                h71feb2d_0    conda-forge
tzlocal                   5.0.1           py311h38be061_0    conda-forge
urllib3                   2.0.3              pyhd8ed1ab_0    conda-forge
wcwidth                   0.2.6              pyhd8ed1ab_0    conda-forge
wheel                     0.40.0             pyhd8ed1ab_0    conda-forge
xorg-kbproto              1.0.7             h7f98852_1002    conda-forge
xorg-libice               1.0.10               h7f98852_0    conda-forge
xorg-libsm                1.2.3             hd9c2040_1000    conda-forge
xorg-libx11               1.8.6                h8ee46fc_0    conda-forge
xorg-libxau               1.0.11               hd590300_0    conda-forge
xorg-libxdmcp             1.1.3                h7f98852_0    conda-forge
xorg-libxext              1.3.4                h0b41bf4_2    conda-forge
xorg-libxrender           0.9.10            h7f98852_1003    conda-forge
xorg-libxt                1.3.0                hd590300_0    conda-forge
xorg-renderproto          0.11.1            h7f98852_1002    conda-forge
xorg-xextproto            7.3.0             h0b41bf4_1003    conda-forge
xorg-xproto               7.0.31            h7f98852_1007    conda-forge
xz                        5.4.2                h5eee18b_0
zeromq                    4.3.4                h9c3ff4c_1    conda-forge
zipp                      3.15.0             pyhd8ed1ab_0    conda-forge
zlib                      1.2.13               hd590300_5    conda-forge
zstd                      1.5.5                hc292b87_0

Environment info

active environment : rpy2
    active env location : /workspace/.conda/rpy2
            shell level : 3
       user config file : /home/hschult/.condarc
 populated config files : /opt/miniconda/.condarc
                          /home/hschult/.condarc
          conda version : 4.14.0
    conda-build version : not installed
         python version : 3.7.12.final.0
       virtual packages : __linux=4.19.0=0
                          __glibc=2.28=0
                          __unix=0=0
                          __archspec=1=x86_64
       base environment : /opt/miniconda  (read only)
      conda av data dir : /opt/miniconda/etc/conda
  conda av metadata url : None
           channel URLs : https://conda.anaconda.org/bioconda/linux-64
                          https://conda.anaconda.org/bioconda/noarch
                          https://conda.anaconda.org/conda-forge/linux-64
                          https://conda.anaconda.org/conda-forge/noarch
                          https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /opt/miniconda/pkgs
                          /home/hschult/.conda/pkgs
       envs directories : /workspace/.conda
                          /home/hschult/.conda
                          /home/hschult/.conda/envs
                          /opt/miniconda/envs
               platform : linux-64
             user-agent : conda/4.14.0 requests/2.28.1 CPython/3.7.12 Linux/4.19.0-23-amd64 debian/10 glibc/2.28
                UID:GID : 1012:1012
             netrc file : None
           offline mode : False
mfansler commented 1 year ago

Thanks for filing this!

I think we need specifics on exactly how it was registered as a kernel, where is Jupyter installed (user level?), and what environment is active when Jupyter is launched.

Essentially, r-base package is responsible for setting R_HOME when the environment is activated. But if Jupyter only recognizes this kernel as a Python kernel, then it probably won't run the environment's activation scripts to initialize these values.

hschult commented 1 year ago

Thank you for the explanation.

To answer your questions: I registered the kernel by running the following command after activating the "rpy2" environment from above. python -m ipykernel install --user --name rpy2 --display-name "rpy2" I could find out that Jupyter is located in my base environment which is in /opt. So it should be root level and base is probably active when Jupyter is launched. Though, I would have to ask my IT to get more details.

I hope that helps and thanks for looking into it.

Gloriacyy commented 1 month ago

@hschult, the same problem confused me, thanks for your answer!

import os
os.environ["R_HOME"] = "/workspace/.conda/rpy2/lib/R"

import rpy2.robjects``

it worked