Specify reticulate python path in RETICULATE_PYTHON environment variable
Asked Answered
H

3

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?

Holomorphic answered 21/1, 2020 at 13:29 Comment(7)
It looks like RStudio is still loading the python version in your PATH. Try the hints here: rstudio.github.io/reticulate/articles/versions.htmlTriumvirate
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 cumbersomeHolomorphic
Does setting the environment variable in .bashrc have same effect as setting it in .Rprofile?Triumvirate
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.Pourparler
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 RStudioHolomorphic
I believe you need to restart your machine after changing .profile or maybe re-loginPourparler
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?Nika
M
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)
Manifold answered 13/10, 2020 at 9:22 Comment(0)
M
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

Masticate answered 9/2, 2022 at 4:37 Comment(0)
E
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.

Edaedacious answered 14/1, 2022 at 21:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.