Multiple conda enviroments in (R) functions using reticulate
For an internally used R package I need to have certain functions load python environments to do part of the processing. The environment cannot be the same one for all functions, unfortunately, due to python package conflicts. So I need to find out how to attach to different conda environments.
But I actually fail before that, as what I get when I attach an env:
reticulate::use_condaenv(pyannote)
ERROR: The requested version of Python ('/Users/frkkan96/opt/anaconda3/envs/pyannote/bin/python') cannot be used, as another version of Python
('/Users/frkkan96/Library/r-miniconda/envs/r-reticulate/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(python, required = required) :
failed to initialize requested version of Python
library(reticulate)
reticulate::use_condaenv(pyannote)
ERROR: The requested version of Python ('/Users/frkkan96/opt/anaconda3/envs/pyannote/bin/python') cannot be used, as another version of Python
('/Users/frkkan96/Library/r-miniconda/envs/r-reticulate/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(python, required = required) :
failed to initialize requested version of Python
The issues I have then are:
1 - How do I make sure that the correct python is used when I load a conda environment? I would have assumed that I could force a re-discover of the python environment somehow after reticulate
has been loaded, but I have so far failed:
py_discover_config(use_environment = pyannote)
python: /Users/frkkan96/Library/r-miniconda/envs/r-reticulate/bin/python
libpython: /Users/frkkan96/Library/r-miniconda/envs/r-reticulate/lib/libpython3.6m.dylib
pythonhome: /Users/frkkan96/Library/r-miniconda/envs/r-reticulate:/Users/frkkan96/Library/r-miniconda/envs/r-reticulate
version: 3.6.13 | packaged by conda-forge | (default, Feb 19 2021, 05:36:16) [GCC Clang 11.0.1]
numpy: /Users/frkkan96/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/numpy
numpy_version: 1.19.5
2 - In my case, I need to be able to switch conda environment to the one appropriate for the R function called. How do I specify the python environment to use on the fly?
I appreciate all the help I can get on these (probably related) isses.
Fredrik
Category Data Science