Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 916
stineb Avatar asked Aug 02 '20 20:08

stineb


1 Answers

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 :

  • https://tensorflow.rstudio.com/installation/
  • https://rstudio.github.io/reticulate/
like image 80
Anugerah Erlaut Avatar answered Sep 19 '22 19:09

Anugerah Erlaut