Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify reticulate python path in RETICULATE_PYTHON environment variable

Whenever I use reticulate in RStudio, the default REPL is using python2.7, but I want to use python3 per default. I have added the python path to python3 to my .bashrc in the environment variable RETICULATE_PYTHON and when I use R and reticulate from the command line, Sys.getenv('RETICUALTE_PYTHON') returns /usr/bin/python3. If open a REPL in the command line using, I get the correct path. If I do the same in RStudio, I get an empty string.

R
Sys.getenv('RETICULATE_PYTHON')

Return in R (from command line):

[1] "/usr/bin/python3"

in RStudio:

[1] ""

In the RStudio Terminal the output is correct:

echo $RETICULATE_PYTHON
/usr/bin/python3

Also, when I start R from the command line, py_config() is this:

> library(reticulate)
> py_config()
python:         /usr/bin/python3
libpython:      /usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6.so
pythonhome:     /usr:/usr
version:        3.6.7 (default, Oct 22 2018, 11:32:17)  [GCC 8.2.0]
numpy:          /usr/lib/python3/dist-packages/numpy
numpy_version:  1.14.5

NOTE: Python version was forced by RETICULATE_PYTHON

But in RStudio it is this:

> library(reticulate)
> py_config()
python:         /usr/bin/python
libpython:      /usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
pythonhome:     /usr:/usr
version:        2.7.15+ (default, Oct  2 2018, 22:12:08)  [GCC 8.2.0]
numpy:          /usr/lib/python2.7/dist-packages/numpy
numpy_version:  1.14.5

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

Restarting RStudio did not help. Any suggestions on how to make RStudio use the correct python binary as well?

like image 882
clemens Avatar asked May 14 '26 14:05

clemens


2 Answers

When faced similar issue I solved it by specifying Python configuration before loading the reticulate package:

Sys.setenv(RETICULATE_PYTHON = "C:\\ProgramData\\Anaconda3")
library(reticulate)
like image 110
nba2020 Avatar answered May 16 '26 02:05

nba2020


The solution I used was simply to set the default Python interpreter for RStudio (2021.09.1 for Windows) using Tools > Global Options to the path of the interpreter I wanted RStudio to use.

In my case, I set it too use "C:/tools/Anaconda3/envs/dev/python.exe".

I assume the same solution would work for Linux too.RStudio Options

like image 33
Rohan Thomas Avatar answered May 16 '26 02:05

Rohan Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!