Open abeburnett opened 5 years ago
Hello,
I am experiencing exactly the same issue when using virtualenv and use_virtualenv(). I add the python virtual environment to RStudio using the .RProfile file
library(reticulate) use_virtualenv("/Users/HMarivate/Documents/my_second_python_project/virt_env", required = TRUE)
RStudio crashes when I use
py_config()
but does not crash when I use py_config()
py_discover_config() python:C:/Users/HMarivate/Documents/my_second_python_project/virt_env/Scripts/python.exe libpython: python38.dll pythonhome: C:/Users/HMarivate/Documents/my_second_python_project/virt_env version:3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] Architecture: 64bit numpy: [NOT FOUND] NOTE: Python version was forced by use_python function
Just adding a note to this to eliminate Conda as the cause of the problem.
Thanks a ton. I also have this issue still.
UPDATES: Check a later comment on an easier fix https://github.com/rstudio/reticulate/issues/456#issuecomment-1046045432
Raise my hands too.
One troubleshoot attempt that might be helpful. I think it's because when reticulate
tries to load numpy
, some C/C++ libraries are not compatible with R? (like BLAS or LAPACK)
reticulate
to use my own conda
environment. If the environment is bare minimal (numpy
not installed), py_config()
works fine:numpy
from within conda
environment, R crashes. However, if I install from pip
(provided by conda
env as well), reticulate works finenumpy
on conda, for example, h5py
, R crashesnumpy
, like django
, R is finenumpy
, however, when I tried to install pandas
from either conda or pip, R crashesEnvironment with this issues:
numpy
(1.22.2, automatically determined by conda and pip)> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.1
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
Environments that seem to be fine:
** Other platforms not tested
After some digging into R manual - shared BLAS, I was able to find the solution. However, this solution is kinda below the average as it changes the BLAS that R uses.
Here are my shell commands:
Step 1: Backup R's default BLAS. On Linux systems, change dylib
to so
cd "/Library/Frameworks/R.framework/Resources/lib/"
mv "libRblas.dylib" "libRblas.dylib.origin"
Step 2: In your conda environment, make sure conda is using openblas by running:
conda install "libblas=*=*openblas"
Step 3: find your conda version of BLAS/LAPACK, usually in your <conda environment>/lib/libopenblas_XXX
, in my case, it's /Users/dipterix/miniconda/envs/my-env/lib/libopenblas_vortexp-r0.3.18.dylib
. Symlink to your R lib:
ln -s "/Users/dipterix/miniconda/envs/my-env/lib/libopenblas_vortexp-r0.3.18.dylib" "/Library/Frameworks/R.framework/Resources/lib/libRblas.dylib"
Step 4: Test
Open R from terminal (Not RStudio, because if BLAS is not linked correctly, RStudio will crash). Run sessionInfo()
. If BLAS is not linked correctly, stats
package won't load, and R will raise this error. If everything works fine, you should be able to see something like this:
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.1
Matrix products: default
BLAS: /Users/dipterix/miniconda/envs/my-env/lib/libopenblas_vortexp-r0.3.18.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib
Now, reticulate
won't crash R
(As of Feb/19 2022, this solution has been tested on OSX only)
Further dig into the reticulate
source code
https://github.com/rstudio/reticulate/blob/3c438a5995fbb1274d52886fdc3d8a71cf1ba718/R/zzz.R#L63-L71
Looks like the devs already provided "fix" to this problem: you just need to set OPENBLAS
and OMP_NUM_THREADS
before loading reticulate
. To do this, edit your ~/.Renviron
, or you could run R command usethis::edit_r_environ()
, and set these environment variables.
Please DO change OPENBLAS
accordingly
OPENBLAS="<path to your conda or venv environment>/lib/libblas.dylib"
OMP_NUM_THREADS=1
Restart your R, now everything works fine.
If you don't know how to find BLAS libpath, activate conda/venv, run python
:
import numpy
numpy.__config__.show()
# blas_info:
# libraries = ['cblas', 'blas', 'cblas', 'blas']
# library_dirs = ['/Users/dipterix/miniconda/envs/my-env/lib']
# include_dirs = ['/Users/dipterix/miniconda/envs/my-env/include']
# ...
Check inside library_dirs
, look for file starting with libblas
(on OSX, it should be libblas.dylib
. On Linux, it should be libblas.so
.
According to conda-forge's newest knowledge base, they compile and ship their libraries using a bundled version of BLAS. By default, reticulate
uses R's default BLAS or /usr/local/opt/openblas
, which is different to the BLAS version used by conda
. When reticulate
tries to load python modules such as numpy
or pandas
using R's BLAS, the incompatibility between different BLAS versions will crash R. This issue often exists on OSX, but could also affect some Linux users. To resolve this issue, reticulate
just need to link to conda
version of BLAS.
@t-kalinowski is it possible that use_condaenv
also detects and change the OPENBLAS
env var?
Using RStudio 1.1.463 and reticulate 1.11.1-9000 I'm having some issues.
Works, and displays:
But, if I do...
Then my R session crashes. However, if I use
py_discover_config()
...That clearly works.
Any idea why py_config() doesn't work after executing
use_condaenv()
?