pythonrrpy2

How to correctly install rpy2 in python?


There are a lot of other questions like mine but they're quite outdated so I think a new updated guide would be helpful for everyone trying to install rpy2 in Python.
In my case, I'm trying to work with the package pymer4 but i couldn't manage to correctly import it.
I found out that the error lies in rpy2.robjects so I decided to follow this answer to create a clean environment

Steps

  1. I created a new conda environment

    conda create -n rpy2_env r-essentials<4.2 r-base<4.2 python=3.7

I set the version to <4.2 because I read online this problem can be caused by a wrong r version. Anyway neither 4.2 nor 4.1 worked for me.

  1. I installed the pymer4 package (and its dependecies, including rpy2)
    conda install -c ejolly -c conda-forge pymer4

  2. I downgraded rpy2 to 3.4.5 (see related question below, I also tried without downgrading)
    conda install rpy2=3.4.5

  3. When i try to import pymer4 or rpy2.robjects the following error shows up

    R[write to console]: Error in gettext(fmt, domain = domain, trim = trim) : 3 arguments passed to .Internal(gettext) which requires 2

In the terminal the logs showed no error during download and installation.
If you need more info about my system or anything else, comment and I will update the question.
I am really stuck at this point, I think i tried everything I could. I would really appreciate if someone could enlighten me.

Similar Questions
Import rpy2 : unable to determine R library path
Rpy2 can't find my R libraries on install
R[write to console]: Error in gettext(fmt, domain = domain, trim = trim) : 3 arguments passed to .Internal(gettext) which requires 2


Solution

  • I think I found the solution.
    The problem is with conda and rpy2, apparently rpy2 installed with conda is outdated.

    If you want to install rpy2, do not use the conda package manager and just use the pip package manager in a new environment (venv or conda env), and everything will work fine. The correct way to install rpy thus is:

    pip install rpy2
    

    Be sure not to use conda install for other packages in the same environment where you installed rpy2 with pip to avoid possible conflicts.