0
$\begingroup$

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

$\endgroup$
1
  • 1
    $\begingroup$ I don't think it is possible (currently) to load multiple Python instances in the same R session. Maybe you can try running multiple R sessions with some shell script rather than handling the logic in a package? For #1, you can try to force reticulate to use the right env by setting the RETICULATE_PYTHON environment variable. $\endgroup$
    – Adam
    Apr 16, 2022 at 5:54

1 Answer 1

0
$\begingroup$

It seems that this is not possible to do at the moment. Only one python conda env can be active, and most likely needs to be set up in .Rprofile.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.