如何从R中的网状结构切换virtualenv? 简而言之:通过重新启动R会话!一旦选择,您将无法在reticulate中切换virtualenv!

问题描述

R包中的网状结构中有一个use_virtualenv函数,但看起来我无法使用不同的virtualenvs对其进行两次调用,因此始终忽略第二次调用

是否有一种方法可以停用第一个virtualenv,以便我可以按预期的行为呼叫use_virtualenv("venv2")

#initialize
require(reticulate)
virtualenv_create("venv1")
virtualenv_create("venv2")

#call first virtualenv
use_virtualenv("venv1") 
py_config() #show venv1 specs

#call second vrtualenv
use_virtualenv("venv2")
py_config() # still show venv1 specs,I want venv2 here

我认为unloadNamespace("reticulate")可以工作,但就我而言,第一次调用是由另一个程序包进行的。

解决方法

简而言之:通过重新启动R会话!一旦选择,您将无法在reticulate中切换virtualenv!

我尝试过(但首先选择了"venv2")。


> use_python("venv1",T)
Error in use_python("venv1",T) : 
  Specified version of python 'venv1' does not exist.
> use_python("~/.virtualenvs/venv1",T)
ERROR: The requested version of Python ('~/.virtualenvs/venv1') cannot
be used,as another version of Python
('/home/josephus/.virtualenvs/venv2/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("~/.virtualenvs/venv1",T) : 
  failed to initialize requested version of Python

网状消息,以至于必须启动一个新会话以选择新的虚拟环境。 尽管它不像use_virtualenv(<xxx>,T)那样冗长,但这也必须适用于use_python(<xxx>,T)