如何从 R 中的网状结构切换 virtualenv?

How can I switch virtualenv from reticulate in R?

在 R 包 reticulate 中有一个函数 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).

那样冗长