无法通过 conda、virtualenv 或 py_install 安装带有 reticulate 的 tensorflow

Can't install tensorflow with reticulate via conda, virtualenv, or py_install

我在尝试使用 tensorflow 时 运行 遇到了一些 reticulate 麻烦。如果我在 CLI 上 运行 普通 R 会话或普通 Python 会话,我可以安装和使用 tensorflow。

library(reticulate)                         # loads fine
conda_create("r-reticulate")                # seems to work
conda_install("r-reticulate", "scipy")      # seems to work
conda_install("r-reticulate", "tensorflow") # fails
# Collecting package metadata (current_repodata.json): ...working... done
# Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.
# Solving environment: ...working... failed with repodata from current_repodata.json, will retry with next repodata source.
# 
# ResolvePackageNotFound: 
#   - python=3.1
# 
# Error: one or more Python packages failed to install [error code 1]

不确定那是什么,所以我试试

use_condaenv("r-reticulate")                # seems to work 
py_install("tensorflow")                    # fails

给出错误:

Error: could not find a Python environment for /opt/local/bin/python3.9

然后我尝试了:

virtualenv_create("rstudio-python-virtualenv")

这会引发错误

ERROR: Failed cleaning build dir for numpy

虽然失败了,但为了笑我试过了

virtualenv_install("rstudio-python-virtualenv", "tensorflow") 

抛出

ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)

在进行更多故障排除后,我尝试将“全局选项”中指定的 Python 解释器更改为上面创建的这 2 个新虚拟环境。它对错误消息没有影响。

我也试过 reticulate::py_install("tensorflow") 抛出:

Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.
Solving environment: ...working... failed with repodata from current_repodata.json, will retry with next repodata source.

ResolvePackageNotFound: 
  - python=3.1

Error: one or more Python packages failed to install [error code 1]
>

显然,至少部分问题是 conda_create 正在创建基于 Python 版本的环境,这些版本对于它试图安装的 tensorflow 版本来说太新了(即 Python v3.10.0)。这有效:

conda_create("r-py-conda-3.6", python_version = "3.6")
conda_install(envname = "r-py-conda-3.6", 
              packages=("tensorflow"))