2

I am aware that similar questions have been asked before, but I either don't understand the answers, or there aren't any; so I decided to describe my problem in as much detail as possible.

Problem: RStudio reticulate package uses Python from this path:

"/usr/bin/python"

but I want it to use python from this path - always, as a default:

"/Library/Frameworks/Python.framework/Versions/3.7/bin/python3"

How do I know it happens? I open RStudio, and create a new python script. A new file with an extension .py is generated. I type something in:

import pandas as pd

and execute (by clicking cmd+enter). I then see what happens in the console - the reticulate package is called:

reticulate::repl_python()
Python 2.7.10 (/usr/bin/python) 
Reticulate 1.12 REPL -- A Python interpreter in R.

I would like to permanently change where the reticulate package looks for Python. From the Terminal I know:

$ python --version 
Python 3.7.3
which python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3

So, I would like to tell RStudio to always look in this path to find Python 3.7. I have tried to use the following command, run from an R script:

use_python("/Library/Frameworks/Python.framework/Versions/3.7/bin/python3")

but it doesn't do anything - my naive understanding is that this command is useful within an R markdown file, i.e. when I have code that combines R and Python, in separate chunks. I would like to change the path that is used when a Python script is run from within RStudio. Is there some kind of a config file I could edit?

I hope this makes sense. I am not a regular Python user, only started learning now, and I am also not very good with paths, so I would appreciate step-by-step answers.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Wera
  • 478
  • 3
  • 12
  • I found this question looking for a way to change the python version that the R system() command uses to that used in terminal. For that purpose, these two questions provide the solution: https://stackoverflow.com/questions/36705878/rstudio-python-version-change-on-mac, https://stackoverflow.com/questions/14617041/how-can-i-see-the-current-value-of-my-path-variable-on-os-x. – Phil Jun 21 '21 at 02:43

1 Answers1

13

OK, so I have posted too early - after some more googling I can solve this problem myself, but I think it is worth posting an answer here for people like me (i.e. not path-proficient or python-proficient).

There is something like a config file for R, called .Renviron. In order to access it, use Terminal to go to your home directory (i.e. the one that you go to when you type 'cd'). If you have never used this file before, it might not exist, in which case you need to create it.

Once in your home directory, type:

ls -a

then check on the list of files that appears whether .Renviron is there. Below are instructions what if you already have .Renviron (IF YES), and what if you don't (IF NO).

IF NO, type:

touch .Renviron

which creates the file.

IF YES, just proceed as below (without using the touch command).

Write:

nano .Renviron

the .Renviron file will open. In it, add a line that says:

RETICULATE_PYTHON="enter your desired path here"

so, in my case, this is:

RETICULATE_PYTHON="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3"

now save the file by exiting nano (ctrl+x) and clicking 'y' when it asks whether to save changes (press 'y' then press enter).

restart you RStudio. It should work now. I hope this is useful!

Wera
  • 478
  • 3
  • 12
  • 1
    What do you mean by Terminal? R terminal or windows command prompt? – user1916067 Jul 19 '21 at 12:36
  • @user1916067 This is an answer for a Mac machine, and I mean Mac Terminal. However, there is also Terminal in RStudio for Windows, and I am guessing you can use this one on a Windows Machine. – Wera Jul 22 '21 at 15:42