简体   繁体   English

无法更改网状中的 Python 路径

[英]Unable to change Python path in reticulate

The first lines I run when launching my rstudio session are:我在启动 rstudio 会话时运行的第一行是:

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.但是,当我运行py_config()它显示仍在使用默认的 python 2.7 安装。

This is an issue because I'm unable to import any modules that were installed for python3.这是一个问题,因为我无法导入为 python3 安装的任何模块。 Any idea why this isn't working?知道为什么这不起作用吗? I followed documentation fairly closely.我相当密切地遵循文档。

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.)我观察到 .RProfile 中的技术“use_python('path')”和 Sys.setenv(RETICULATE_PYTHON = 'path') 的策略对我都不起作用(当然我确信它一定对其他人有效。)

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:确实产生了两个可供选择的路径(一个用于安装在我的 mac 上的 python2,另一个用于安装在我的 mac 上的 python3),因此我能够在我的主目录中创建一个“.Renviron”文件,其中包含以下一行:

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.重新启动 RStudio 后, library(reticulate)激活所需的 python3, repl_python()打开 python3 交互窗口等。

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.在第一次使用 reticulate 之前设置 RETICULATE_PYTHON 似乎很重要。

use_python("path/to/python3") definitely does not work , although the Reticulate Python version configuration article says so. use_python("path/to/python3")肯定不行,虽然Reticulate Python 版本配置文章是这么说的。 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:我尝试使用当前的 Reticulate 版本 (1.13) 设置解释器,并且该软件包给了我一个非常诚实的答案:

> 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 :幸运的是,将包含行.Renviron RETICULATE_PYTHON="/path/to/python3".Renviron文件放入用户的主目录确实有效

> 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.您可以通过将RETICULATE_PYTHON行添加到全局Renviron文件来为所有用户配置 Reticulate Python 解释器。 It is usually found in the etc subdirectory of R's home directory.它通常位于 R 主目录的etc子目录中。 You can find out where R's home is by running the R.home() function in the R interpreter.您可以通过在 R 解释器中运行R.home()函数来找出 R 的家在R.home() In my case (Ubuntu 18.04.3 LTS) it was /usr/lib/R , so I edited /usr/lib/R/etc/Renviron .在我的情况下(Ubuntu 18.04.3 LTS)它是/usr/lib/R ,所以我编辑了/usr/lib/R/etc/Renviron You obviously need admin rights to do this.您显然需要管理员权限才能执行此操作。

The only thing that work for me on Mac OSX, perform the following commands in the terminal:在 Mac OSX 上唯一对我有用的是在终端中执行以下命令:

touch $HOME/.Renviron

Then open it, I use vim, so my command is then the following:然后打开它,我使用vim,所以我的命令如下:

vim $HOME/.Renviron

Add the following (for anaconda):添加以下内容(对于 anaconda):

RETICULATE_PYTHON="/anaconda3/bin/python"

Otherwise, in the terminal type: which python3 and enter your output path否则,在终端输入: which python3并输入您的输出路径

RETICULATE_PYTHON="your path from which python3"

Install numpy in the python environment you wish to use.在你要使用的python环境中安装numpy

pip install numpy

I found that Reticulate refuses to use a version of python that does not have numpy installed.我发现 Reticulate 拒绝使用没有安装numpy的 python 版本。

I figured this out by running:我通过运行发现了这一点:

library("reticulate")
py_discover_config()

Reticulate skipped the first two version of python listed and used the third. Reticulate 跳过了列出的前两个 python 版本并使用了第三个。 I noticed it printed out the numpy version so it was probably looking for it as a requirement.我注意到它打印出numpy版本,因此它可能正在寻找它作为要求。 It is not mentioned in the docs and should probably be added as a common problem. 文档中没有提到它,可能应该作为一个常见问题添加。

It worked for me:它对我有用:

  • Sys.setenv(RETICULATE_PYTHON = "C:\\ProgramData\\Anaconda3") Sys.setenv(RETICULATE_PYTHON = "C:\\ProgramData\\Anaconda3")
  • library(reticulate)图书馆(网状)
  • repl_python() repl_python()

For windows users, after creating a virtualenv python :对于 Windows 用户,在创建virtualenv python 之后

virtualenv python

this worked to use the virtualenv这有助于使用 virtualenv

Sys.setenv(RETICULATE_PYTHON = "python/Scripts/python.exe")
library(reticulate)
py_config()

EDIT: This works for any path, eg编辑:这适用于任何路径,例如

Sys.setenv(RETICULATE_PYTHON = "C:/Users/UserA/Anaconda3/envs/myEnv/python.exe")

To avoid having to do it each time, add the Sys.setenv line to RProfile:为了避免每次都这样做,将 Sys.setenv 行添加到 RProfile:

file.edit(file.path("~", ".Rprofile"))
file.edit(".Rprofile")

You have to discover the path to your the .exe python file and then specify it in the .Renviron file.您必须找到 .exe python 文件的路径,然后在 .Renviron 文件中指定它。

The best is to create a conda environment to your project and then specify it as your reticulate path.最好是为你的项目创建一个 conda 环境,然后将其指定为你的网状路径。

library(reticulate)

conda_create("environment name", packages = c("pandas", "matplotlib", "seaborn"), conda = "auto") # creates conda environment for your project

conda_python("environment name") # returns the .exe python path for your environment

Keep this path for the .Renviron file!!保留 .Renviron 文件的路径!!

To edit the .Renviron file:要编辑 .Renviron 文件:

library(usethis)

edit_r_environ()

Then, the .Renviron file will open.然后,.Renviron 文件将打开。 Paste the .exe path in it as follows:将 .exe 路径粘贴到其中,如下所示:

RETICULATE_PYTHON="YOUR ENVIRONMENT PATH/python.exe"

Remember to paste it with / instead of "\\".记住用 / 而不是“\\”粘贴它。

Restart the R session and use the following command to check if everything is right:重新启动 R 会话并使用以下命令检查是否一切正常:

library(reticulate)

py_config()

If everything is right, you should get something like:如果一切正常,您应该得到如下信息:

python: YOUR ENVIRONMENT PATH/python.exe libpython: YOUR ENVIRONMENT PATH/python39.dll pythonhome: YOUR ENVIRONMENT PATH version: 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)] Architecture: 64bit numpy: YOUR ENVIRONMENT PATH\\lib\\site-packages\\numpy numpy_version: 1.21.2 python:您的环境路径/python.exe libpython:您的环境路径/python39.dll pythonhome:您的环境路径版本:3.9.7(默认,2021 年 9 月 16 日,16:59:28)[MSC v.1916 64 位(AMD64) )] 架构:64 位 numpy:您的环境路径\\lib\\site-packages\\numpy numpy_version:1.21.2

After this it should work :)在此之后它应该可以工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM