rPython using wrong python installation on Mac OSX

2019-01-15 19:20发布

问题:

I've installed python 2.7.8 alongside the 2.7.5 which comes with OSX 10.9.4.

Now how can I point rPython to python 2.7.8?

Attempt #1

I've modified the OSX .bash_profile as follows to point everything to the newer python installation.

export PATH=/usr/local/Cellar/python/2.7.8/bin/:$PATH:usr/local/bin:

And now when I run python from the terminal, it correctly runs the newer version

mba:~ tommy$ which python
/usr/local/Cellar/python/2.7.8/bin//python

However, rPython, still sees 2.7.5.

> library(rPython)
Loading required package: RJSONIO
> python.exec("import sys; print(sys.version)")
2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]

Attempt #2

It looks like the .bash_profile doesn't get used by R at all... so I've tried to modify the PATH within R. But still no luck.

> Sys.getenv("PATH")
[1] "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
> Sys.setenv(PATH = "usr/local/Cellar/python/2.7.8/bin")
> library(rPython)
Loading required package: RJSONIO
> python.exec("import sys; print(sys.version)")
2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]

Attempt #3

I tried removing and re-installing the rPython package thinking perhaps it was using the version of Python that it found upon installation. No luck either.

Attempt #4

I've tried installing from source to see if that does anything... no luck.

Update

Okay so it looks like the problem isn't anything to do with rPython itself.

http://cran.r-project.org/web/packages/rPython/INSTALL

Package rPython depends on Python (>= 2.7).

It requires both Python and its headers and libraries. These can be found in python and python-dev packages in Debian-like Linux distributions.

In systems where several Python versions coexist, the user can choose the Python version to use at installation time. By default, the package will be installed using the Python version given by

$ python --version

When I run that in the terminal..

mba:src tommy$ python --version
Python 2.7.8

But when I run it in R...

> system("python --version")
Python 2.7.5

So the problem is simply that R doesn't use OSX's .bash_profile. I'll need to figure out how to change PATH outside of .bash_profile, or get R to use .bash_profile.

What else can I try to get rPython working with 2.7.8?

回答1:

I setup the following commands in RStudio, hope these help.

> system("python --version")
Python 2.7.10

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

> system("python --version")
Python 2.7.11


回答2:

I know this is an old question, but I ran into the same problem as OP and this is the solution I found worked.

First, I added the RPYTHON_PYTHON_VERSION=3 to my ~/.bash_profile. Instead of installing rPython using install.packages, I downloaded the source from CRAN and installed from the command line using R CMD INSTALL. This worked fine and detected the python3 version I had installed on my system.



回答3:

You can have a look at the rPython INSTALL file (sorry, perhaps I should make it more explicit). It has a section on how to install rPython using the desired Python version when several coexists. It says:

In systems where several Python versions coexist, the user can choose the Python version to use at installation time. By default, the package will be installed using the Python version given by

$ python --version

but it is possible to select a different one if the RPYTHON_PYTHON_VERSION environment variable is appropriately set.

For instance, if it is defined as

 RPYTHON_PYTHON_VERSION=3.2

it will try to use Python 3.2 (looking for python3.2 and python3.2-config in the path). If set to

 RPYTHON_PYTHON_VERSION=3

it will install against the "canonical" Python version in the system within the 3.x branch.



回答4:

I came across this post trying to get R to use a desired Python version and found a solution which worked for me (using Rstudio on OSX Yosemeti).

Here is the Python my terminal uses, which is the one I want R to find:

$ which python
/usr/local/bin/python

In R, system('which python') produces /usr/bin/python. I can see that this is because /usr/bin is to the right of /usr/local/bin in Sys.getenv('PATH').

I tried changing my PATH in ~/.Renviron or ~/.Rprofile but it wasn't fixing the problem. This is because there was another Rprofile file running right before kernel startup completed.

To find all the Rprofile files on my system, I ran these commands:

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

One of the files -- R_HOME/library/base/R/Rprofile -- contained some code which said it dealt with PATH conflicts on my OS. I guess it chose the wrong PATH of the possible choices. Adding the following line to the end of that file, showed me the correct path in Rstudio after restarting my R kernel:

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


回答5:

what worked for me is that i specify the lib and version in install.packages, like this

install.packages("rPython",lib= "path_to_your_R/R/library/3.1", configure.vars= "RPYTHON_PYTHON_VERSION=3")

And it indeed installed with python version which under 3.x



标签: r rpython