8

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?

clemens
  • 6,653
  • 2
  • 19
  • 31
  • It looks like RStudio is still loading the python version in your PATH. Try the hints here: https://rstudio.github.io/reticulate/articles/versions.html – meenaparam Jan 21 '20 at 13:50
  • Exactly, usually setting the RETICULATE_PYTHON variable solves that issue, but it doesn't work for some reason. of course i can set the path every time i use reticulate, but that's too cumbersome – clemens Jan 21 '20 at 13:52
  • Does setting the environment variable in `.bashrc` have same effect as setting it in `.Rprofile`? – meenaparam Jan 21 '20 at 13:54
  • have you tried adding `export RETICULATE_PYTHON=/usr/bin/python3 ` to .profile, based on https://help.ubuntu.com/community/EnvironmentVariables : ```Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.``` – DS_UNI Jan 21 '20 at 14:11
  • although all options (.bashrc, .profile, .Renviron) work for R from the command line, only adding the line `Sys.setenv('RETICULATE_PYTHON'='/usr/bin/python3')` works for RStudio – clemens Jan 21 '20 at 14:40
  • I believe you need to restart your machine after changing .profile or maybe re-login – DS_UNI Jan 22 '20 at 08:26
  • I'm having a similar issue - I created a ~/.Rprofile file, and added a Sys.setenv('RETICULATE_PYTHON'='path/to/python') And now I can call the R cli and get the correct output from py_config() However, when I run shiny apps on this machine, I still get errors like this: ModuleNotFoundError: No module named 'boto3' The only solution I've found so far is to put use_condaenv("path/to/python") in each R file. Is there an additional step to get shiny server to use this change? – Zack Jan 17 '22 at 15:55

3 Answers3

7

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

Rohan Thomas
  • 81
  • 1
  • 1
6

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)
nba2020
  • 618
  • 1
  • 8
  • 22
1

The documentation says to set it in .Rprofile with

Sys.setenv(RETICULATE_PYTHON="/path/to/your/preferred/python")

if you always want a fixed Python. Otherwise, do as @nba2020 suggests to set it in a particular script.

merv
  • 67,214
  • 13
  • 180
  • 245