Search code examples
rvirtualenvreticulate

Failing to load package using reticulate::use_virtualenv


I have been trying to run some python code via R reticulate with no success. I tried creating a virtual environment and install two packages in it: numpy and a GitHub package called scrublet.

library(reticulate)
if(!"r-scrublet" %in% virtualenv_list()) {
  virtualenv_create("r-scrublet")
  virtualenv_install("r-scrublet", c("numpy","git+https://github.com/AllonKleinLab/scrublet.git"))
}
use_virtualenv(virtualenv = "r-scrublet", required = T)

Then I want to activate the environment and run code over it. When I run py_discover_config() I get output:

python:         C:/Users/gilad/Documents/.virtualenvs/r-scrublet/Scripts/python.exe
libpython:      C:/Users/gilad/Documents/.virtualenvs/r-scrublet/Scripts/python36.dll
pythonhome:     C:/Users/gilad/Documents/.virtualenvs/r-scrublet
version:        3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:/Users/gilad/Documents/.virtualenvs/r-scrublet/Lib/site-packages/numpy
numpy_version:  1.19.1

NOTE: Python version was forced by use_python function

which seems to be as I want. The problem is that I don't seem to be able to actually use this environment. If I test weather numpy is available (py_numpy_available()) I get that not and when I try any import statement (either to one of the installed packages or even to something like os) R crashes with:

R Session Aborted. R encountered a fatal error. The session was terminated

I tried the different reticulate manuals (here and here) and different git issues or SO questions but couldn't still figure this out.


Solution

  • From the installation path you provide, it looks like you're using Windows.
    If you refer to virtualenv doc :

    Virtual environment functions are not supported on Windows (the use of conda environments is recommended on Windows).

    Try:

    library(reticulate)
    conda_create("r-scrublet")
    conda_install(envname="r-scrublet", packages ="numpy","pip","git")
    
    conda_python(envname =  "r-scrublet")
    

    The scrublet.git can be added by running from the r-scrublet environment root folder:

    Scripts\pip install git+https://github.com/AllonKleinLab/scrublet.git
    

    or directly from R:

    conda_install(envname='r-scrublet','git+https://github.com/AllonKleinLab/scrublet.git',pip=T)
    

    provided that you apply this patch. To avoid the patch, you can probably to add \r-scrublets\Library\bin to system path.


    For completeness of snippet, once setup is ready, specify it's use by:

    use_miniconda("r-scrublet", required=T)
    scrub <- import("scrublet")