Unable to change python path in reticulate (R)

2020-02-10 04:43发布

The first lines I run when launching my rstudio session are:

library(reticulate)
use_python("/usr/local/lib/python3.6/site-packages")

However, when I run py_config() it shows as still using the default python 2.7 installation.

This is an issue b.c I'm unable to import any modules that were installed for python3. Any idea why this isn't working? I followed documentation fairly closely.

4条回答
霸刀☆藐视天下
2楼-- · 2020-02-10 04:48

I observed that neither the technique "use_python('path')" nor the tactic of Sys.setenv(RETICULATE_PYTHON = 'path') in .RProfile worked for me (of course I am sure it must have worked for others.)

In any case the line at terminal,

which -a python python3

did produce two paths to choose from (one for python2 and one for python3 installed on my mac), so then I was able to create a ".Renviron" file in my home directory with this single line in it:

RETICULATE_PYTHON="/usr/local/bin/python3"

After I restarted RStudio, library(reticulate) activates the desired python3, and repl_python() opens a python3 interactive window, etc. etc.

查看更多
戒情不戒烟
3楼-- · 2020-02-10 04:52

use_python("path/to/python3") definitely does not work, although the Reticulate Python version configuration article says so. Don't believe it! :-)

I have tried to set the interpreter with the current Reticulate version (1.13), and the package gave me a very honest answer:

> library("reticulate")
> repl_python()
Python 2.7.15 (/usr/bin/python)
Reticulate 1.13 REPL -- A Python interpreter in R.
> use_python('/usr/bin/python3', require=T)
ERROR: The requested version of Python ('/usr/bin/python3') cannot be
used, as another version of Python ('/usr/bin/python') has already been
initialized. Please restart the R session if you need to attach
reticulate to a different version of Python.
Error in use_python("/usr/bin/python3", require = T) : 
  failed to initialize requested version of Python

Luckily, putting a .Renviron file containing the line RETICULATE_PYTHON="/path/to/python3" into the user's home directory does work:

> library("reticulate")
> py_config()
python:         /usr/bin/python3
libpython:      /usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6.so
pythonhome:     /usr:/usr
version:        3.6.8 (default, Oct  7 2019, 12:59:55)  [GCC 8.3.0]
numpy:           [NOT FOUND]

NOTE: Python version was forced by RETICULATE_PYTHON

Finally, here comes the added value of my answer:

You can configure the Reticulate Python interpreter for all users by adding the RETICULATE_PYTHON line to the global Renviron file. It is usually found in the etc subdirectory of R's home directory. You can find out where R's home is by running the R.home() function in the R interpreter. In my case (Ubuntu 18.04.3 LTS) it was /usr/lib/R, so I edited /usr/lib/R/etc/Renviron. You obviously need admin rights to do this.

查看更多
一纸荒年 Trace。
4楼-- · 2020-02-10 05:01

The only thing that work for me on Mac OSX, perform the following commands in the terminal:

touch $HOME/.Renviron

Then open it, I use vim, so my command is then the following:

vim $HOME/.Renviron

Add the following (for anaconda):

RETICULATE_PYTHON="/anaconda3/bin/python"

Otherwise, in the terminal type: which python3 and enter your output path

RETICULATE_PYTHON="your path from which python3"
查看更多
Explosion°爆炸
5楼-- · 2020-02-10 05:10

It worked for me:

Sys.setenv(RETICULATE_PYTHON = "/usr/bin/python3")
library(reticulate)

It seems important that you set RETICULATE_PYTHON before you first use reticulate.

查看更多
登录 后发表回答