Unable to change python path in reticulate (R)

大憨熊 提交于 2019-11-30 02:58:46

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/50145643/unable-to-change-python-path-in-reticulate-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!