Search code examples
pythonmacosrpython

Unable to locate to the right version of python on Mac


The big picture:

I am trying to let rPython package run python scripts that have packages such as numpy, scipy, and scikit-learn.

Now, if that is an impossible task, please let me know right away, because I have spent over 10 hours on this problem (finally switched to a Mac so rPython is installable and runs the most basic python script). But I am new to iOS.

The Question:

After switching to a Mac, I installed rPython and the latest R, RStudio and Anaconda 2. It turned out that Mac has another version of python installed on it.

I tried the following script to change the directory to the python from Anaconda 2:

library(rPython)

Sys.setenv(PATH = paste("/Usr/local/bin", Sys.getenv("PATH"), sep=":"))

But it still is showing the older version of python.

I also checked the version and path of the Anaconda 2 python with which python in the terminal and got the following output:

Last login: Fri Mar 10 11:23:19 on console

Yangs-MacBook-Pro:~ yanghuang$ which python

/Users/yanghuang/anaconda/bin/python

Yangs-MacBook-Pro:~ yanghuang$ python

Python 2.7.13 |Anaconda 4.3.1 (x86_64)| (default, Dec 20 2016, 23:05:08) 

[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin

Any help is appreciated!


Solution

  • If I don't misunderstand your meaning you should use this script:

    library(rPython)
    
    Sys.setenv(PATH = paste("/Users/yanghuang/anaconda/bin/python", Sys.getenv("PATH"), sep=":"))
    

    Also you can run this command in Rstudio:

    > Sys.setenv(PATH = paste("/Users/yanghuang/anaconda/bin/python", Sys.getenv("PATH"),sep=":"))
    
    > system("python --version")
    Python 2.7.13
    

    Another way:

    sudo find /usr -name "*Rprofile*" -print
    

    Find the Rprofile file and add this line then restart R:

    .Internal(Sys.setenv("PATH", paste("/Users/yanghuang/anaconda/bin/python", Sys.getenv("PATH"), sep=":")))