Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RStudio Python Version Change on Mac

My question is:
How can I change the python version to anaconda 3.5 and can use modules that have installed in anaconda in RStudio.

I'm using RStudio Version 0.99.891
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.3 (El Capitan)

and I have installed Anaconda3, set python 3.5 as default python version.
$ python
Python 3.5.1 |Anaconda custom (x86_64)| (default, Dec 7 2015, 11:24:55)

However, in RStudio,

system("python --version")
Python 2.7.10

I tried to change it by following method:

Sys.setenv(PATH = paste("/Users/UserName/anaconda3/bin", Sys.getenv("PATH"), sep=":"))
system("python --version")
Python 3.5.1 :: Anaconda custom (x86_64)

It seems good, but actually it is not.

python.exec("import os")
python.exec("import inspect")
python.exec("inspect.getfile(os)")
python.exec("print(inspect.getfile(os))")
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc

python.exec("import pandas as pd")
Error in python.exec("import pandas") : No module named pandas

Thanks.

like image 323
Qinqing Liu Avatar asked Apr 18 '16 23:04

Qinqing Liu


2 Answers

Put the following line in your .Rprofile:

Sys.setenv(PATH = paste("/home/your_user_name/anaconda3/bin", Sys.getenv("PATH"), sep=":")) 

Proof:

> system("python --version")
Python 3.6.1 :: Anaconda custom (64-bit)
like image 145
Sergey Bushmanov Avatar answered Sep 29 '22 03:09

Sergey Bushmanov


Just to complete @Sergey answer and for anyone who needs it, you can edit .Rprofile directly from R with the following command:

library(devtools)
usethis::edit_r_profile()
like image 35
pedro magalhaes Avatar answered Sep 29 '22 01:09

pedro magalhaes