【问题标题】:Python in R - Error: could not find a Python environment for /usr/bin/pythonR 中的 Python - 错误:找不到 /usr/bin/python 的 Python 环境
【发布时间】:2020-11-22 23:57:33
【问题描述】:

我不明白 R 如何处理 Python 环境和 Python 版本,并不断收到错误 Error: could not find a Python environment for /usr/bin/python。我安装了 Miniconda 并在 shell 中创建了一个 conda 环境:

conda activate r-reticulate

然后,在 R 中,我尝试安装 keras(与包 tensorflow 相同的问题):

library(keras)
reticulate::use_condaenv()
install_keras(method = "conda", conda = reticulate::conda_binary())

...并得到以下错误:

Error: could not find a Python environment for /usr/bin/python

我试图弄清楚 Python R 应该使用什么

reticulate::py_config()

得到

python:         /usr/bin/python
libpython:      /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/libpython2.7.dylib
pythonhome:     /System/Library/Frameworks/Python.framework/Versions/2.7:/System/Library/Frameworks/Python.framework/Versions/2.7
version:        2.7.16 (default, Jul  5 2020, 02:24:03)  [GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.21) (-macos10.15-objc-
numpy:          /Users/bestocke/Library/Python/2.7/lib/python/site-packages/numpy
numpy_version:  1.16.6
tensorflow:     [NOT FOUND]

python versions found: 
 /usr/bin/python3
 /usr/local/bin/python3
 /usr/bin/python

我不明白这一点。这似乎使用的是 Python 2.7。当试图弄清楚 shell 中使用了哪个 Python 时,我得到:

> which python
/opt/miniconda3/envs/r-reticulate/bin/python

> ls -l /opt/miniconda3/envs/r-reticulate/bin/python
lrwxr-xr-x  1 username  wheel  9 Aug  2 15:21 /opt/miniconda3/envs/r-reticulate/bin/python -> python3.6

建议使用 Python 3.6。

我在这里做错了什么?

【问题讨论】:

    标签: python r tensorflow keras reticulate


    【解决方案1】:

    尝试按照https://tensorflow.rstudio.com/installation/ 的指南进行操作:

    在您的 R-studio 控制台中:

    1. install.packages("tensorflow")

    2. library(tensorflow)

    3. install_tensorflow()

    如果您尚未手动安装 Anaconda / Miniconda,则在第 1 步安装。 3、提示会询问你是否允许安装Miniconda。如果你已经安装了conda,那么:

    1. 在 conda 中创建新环境 r-reticulateconda create -n r-reticulate
    2. 使用参数从 R-studio 控制台安装 tensorflow:install_tensorflow(method = 'conda', envname = 'r-reticulate')
    3. 加载网状包library(reticulate)
    4. 在 R-studio use_condaenv('r-reticulate') 中激活 conda 环境
    5. 加载张量流库library(tensorflow)
    6. 检查张量流是否处于活动状态tf$constant("Hellow Tensorflow")

    参考资料:

    【讨论】:

    • 谢谢。我最终不得不在 R 之外直接在终端中安装 python 包 tensorflow 和 keras。这是必要的,因为由于版本不兼容而引发的错误。我做了你建议的一切,除了第 2 点,我在 shell 中由conda install tensorflow(安装 v. 2.2.0)和conda install keras(安装 v. 2.4.3)完成。
    【解决方案2】:

    我使用virtualenv 安装,我发现我必须通过envname 指定环境的完整路径。它不适用于method="virtualenv", envname="r-reticulate"

    【讨论】:

      【解决方案3】:

      我希望有时间补充信息。我尝试按照 Anugerah Erlaut 所说的去做,但尝试通过 WSL 在 R-studio Server 上安装 Keras GPU。

      我知道该解决方案会起作用,因为我在另一台计算机上尝试过,但安装在 Windows 和 Keras CPU 上。经过一段时间的测试(并感到沮丧),似乎 Rstudio 没有更改 r-reticulate 环境的权限。

      所以,我尝试了在 bash 命令行上运行或“纯 R”的解决方案,它工作得很好!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-15
        • 2018-01-15
        • 1970-01-01
        • 2022-06-29
        • 1970-01-01
        • 1970-01-01
        • 2014-05-05
        • 2010-11-13
        相关资源
        最近更新 更多