14

I don't understand how R handles the Python environment and Python version and keep getting the error Error: could not find a Python environment for /usr/bin/python. I installed Miniconda and created a conda environment in the shell:

conda activate r-reticulate

Then, in R, I try to install keras (same problem with package tensorflow):

library(keras)
reticulate::use_condaenv()
install_keras(method = "conda", conda = reticulate::conda_binary())

... and get the following error:

Error: could not find a Python environment for /usr/bin/python

I tried to figure out what Python R should be using by

reticulate::py_config()

and get

python:         /usr/bin/python
libpython:      /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/libpython2.7.dylib
pythonhome:     /System/Library/Frameworks/Python.framework/Versions/2.7:/System/Library/Frameworks/Python.framework/Versions/2.7
version:        2.7.16 (default, Jul  5 2020, 02:24:03)  [GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.21) (-macos10.15-objc-
numpy:          /Users/bestocke/Library/Python/2.7/lib/python/site-packages/numpy
numpy_version:  1.16.6
tensorflow:     [NOT FOUND]

python versions found: 
 /usr/bin/python3
 /usr/local/bin/python3
 /usr/bin/python

I don't understand this. This seems to be using Python 2.7. When trying to figure out which Python is being used in the shell, I get:

> which python
/opt/miniconda3/envs/r-reticulate/bin/python

and

> ls -l /opt/miniconda3/envs/r-reticulate/bin/python
lrwxr-xr-x  1 username  wheel  9 Aug  2 15:21 /opt/miniconda3/envs/r-reticulate/bin/python -> python3.6

Suggesting Python 3.6 should be used.

What am I getting wrong here?

stineb
  • 461
  • 1
  • 5
  • 11

3 Answers3

26

Try to follow the guide at https://tensorflow.rstudio.com/installation/:

In your R-studio console :

  1. install.packages("tensorflow")

  2. library(tensorflow)

  3. install_tensorflow()

If you have not installed Anaconda / Miniconda manually, then at step no. 3, a prompt will ask your permission to install Miniconda. If you already have conda installed, then :

  1. Create new environment r-reticulate in conda : conda create -n r-reticulate
  2. Install tensorflow from R-studio console with parameters : install_tensorflow(method = 'conda', envname = 'r-reticulate')
  3. Load the reticulate package library(reticulate)
  4. Activate the conda environment in R-studio use_condaenv('r-reticulate')
  5. Load the tensorflow libray library(tensorflow)
  6. Check if tensorflow is active tf$constant("Hellow Tensorflow")

References :

bird
  • 2,938
  • 1
  • 6
  • 27
Anugerah Erlaut
  • 990
  • 13
  • 23
  • 1
    Thank you. I ended up having to install python packages tensorflow and keras outside R, directly in the terminal. This was necessary because of errors raised due to version incompatibilities. I did everything you suggest, except point 2, which I did in the shell by `conda install tensorflow` (installed v. 2.2.0) and `conda install keras` (installed v. 2.4.3). – stineb Aug 17 '20 at 19:59
0

I installed using virtualenv, and I found that I have to specify the full path to the env by envname. It does not work by method="virtualenv", envname="r-reticulate"

Phoenix Mu
  • 648
  • 7
  • 12
-1

I hope there is time to add information. I tried to do what Anugerah Erlaut said, but trying to install Keras GPU on R-studio Server, through WSL.

I knew the solution would work because I tried on another computer, but installing on Windows, and Keras CPU. After testing a while (and get frustrated), it seems that Rstudio does not have permissions to change the r-reticulate env.

So, I tried the solution running or "pure R", on bash command line, and it worked just fine!