9

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?

7
  • It looks like RStudio is still loading the python version in your PATH. Try the hints here: rstudio.github.io/reticulate/articles/versions.html Commented Jan 21, 2020 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 Commented Jan 21, 2020 at 13:52
  • Does setting the environment variable in .bashrc have same effect as setting it in .Rprofile? Commented Jan 21, 2020 at 13:54
  • have you tried adding export RETICULATE_PYTHON=/usr/bin/python3 to .profile, based on 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. Commented Jan 21, 2020 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 Commented Jan 21, 2020 at 14:40

3 Answers 3

8

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)
Sign up to request clarification or add additional context in comments.

Comments

8

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.