IBM Support

how to initialise reticulate library or enable python in RStudio WSL 2.1

Troubleshooting


Problem

 You can try to use this sequence to enable python on WSL2.1 environment.
library(tensorflow)
library(reticulate)
library(keras)
install_tensorflow()
py_config()
reticulate::py_available()
  1. You can get connection timed out error while it is installing tensorflow
  2. You might not have tensorflow and numpy while you to use keras::keras_model_sequential() function

Symptom

You can get connection timed out error while it is installing tensorflow
> library(tensorflow)
> library(reticulate)
> library(keras)
> install_tensorflow()
Creating virtual environment '~/.virtualenvs/r-reticulate' ...
Using python: /opt/conda/bin/python3.7
Collecting pip
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) 
after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7faab9f23a58>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/pip/
You might not have tensorflow and numpy while you to use model <- keras::keras_model_sequential() function
library(reticulate)
> Sys.setenv(PATH='/opt/conda/bin:/opt/conda/envs/R-3.6/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/ibm/dsdriver/bin:/scripts/icp4d:/tmpws:
/home/rstudio-admin/.nvm/versions/node/v10.16.3/bin')
> use_python("/opt/conda/bin/python")
> py_config()
python: /opt/conda/bin/python
libpython: /opt/conda/lib/libpython3.7m.so
pythonhome: /opt/conda:/opt/conda
version: 3.7.3 (default, Mar 27 2019, 22:11:17) [GCC 7.3.0]
numpy: [NOT FOUND]
tensorflow: [NOT FOUND]
python versions found:
/opt/conda/bin/python
/home/wsuser/work/.virtualenvs/r-reticulate/bin/python
/usr/bin/python
> reticulate::py_available()
[1] TRUE
> model <- keras::keras_model_sequential()
Error: Installation of TensorFlow not found.
Python environments searched for 'tensorflow' package:
/opt/conda/bin/python3.7
/opt/conda/bin/python3.7
/usr/bin/python2.7
You can install TensorFlow using the install_tensorflow() function.

Cause

Issue might be with it's find python3.7 and python2.7 versions and it only works on python3.7 environment. 

Environment

IBM Watson Studio Local 2.1 installed on IBM Cloud Private
Component: R Studio 

Resolving The Problem

You could try to use below and see this could overcome connection timed out error
library(reticulate)
Sys.setenv(PATH='/opt/conda/bin:/opt/conda/envs/R-3.6/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/ibm/dsdriver/bin:/scripts/icp4d:/tmpws:/home/rstudio-admin/.nvm/versions/node/v10.16.3/bin')
use_python("/opt/conda/bin/python")
py_config()
reticulate::py_available()
Issue might be with it finds python3.7 and python2.7 versions and it works on python3.7 environment. We address this conflicts in 3.0, but for now you can use above procedure to activate python3.7.

 The above will work for default "admin" user, it might not work for other users. It looks some conflict with python2.7 tensorflow libraries. You can use terminal window and run R and then run below code works fine, but when run on R console it fails.

library(tensorflow)
library(keras)
library(reticulate)
py_config()
reticulate::py_available()
model <- keras::keras_model_sequential()

When you run same code in R console instead of terminal, it hangs and restart R session. We have tested same on CP4D3.0 and it works fine.


Document Location

Worldwide


[{"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SSHGWL","label":"IBM Watson Studio Local"},"ARM Category":[{"code":"a8m0z000000bmw2AAA","label":"Modeling->Notebook - R"}],"ARM Case Number":"TS003352711","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"2.1.x","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Document Information

More support for:
IBM Watson Studio Local

Component:
Modeling->Notebook - R

Software version:
2.1.x

Document number:
6202467

Modified date:
30 April 2020

UID

ibm16202467