RStudio Python Version Change on Mac

2019-06-21 01:22发布

问题:

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.

回答1:

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)


回答2:

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()