我有以下现有的Python环境:
$ conda info --envs
base * /home/ubuntu/anaconda3
tensorflow2_latest_p37 /home/ubuntu/anaconda3/envs/tensorflow2_latest_p37
我想做的是激活tensorflow2_latest_p37
环境,并在R代码中使用它。我尝试了以下代码:
library(reticulate)
use_condaenv( "tensorflow2_latest_p37")
library(tensorflow)
tf$constant("Hello Tensorflow!")
但它未能认清环境:
> library(reticulate)
> use_condaenv( "tensorflow2_latest_p37")
/tmp/RtmpAs9fYG/file41912f80e49f.sh: 3: /home/ubuntu/anaconda3/envs/tensorflow2_latest_p37/etc/conda/activate.d/00_activate.sh: Bad substitution
Error in Sys.setenv(PATH = new_path) : wrong length for argument
In addition: Warning message:
In system2(Sys.which("sh"), fi, stdout = if (identical(intern, FALSE)) "" else intern) :
running command ''/bin/sh' /tmp/RtmpAs9fYG/file41912f80e49f.sh' had status 2
正确的做法是什么?
1条答案
按热度按时间w6mmgewl1#
我发现最可靠的方法是在运行
library(reticulate)
之前设置RETICULATE_PYTHON
系统变量,因为这将加载默认环境,而更改环境似乎有点问题。因此,您应该尝试以下操作:你可以把它放在一个
.Rprofile
文件中,使它成为永久性的。我通常把它放在项目文件夹中,这样它在打开项目时就被计算了。在代码中,它看起来像这样:或者更简单,使用
usethis::edit_r_profile(scope = "project")
(谢谢你@rodrigo-zepeda!)。