reticulate + virtual environment

Hi

I am using reticulate and a virtual environment (not conda) to run Python scripts from RStudio. However, when I try to use my own (existing) virtual environment, reticulate does not use it. If I run my scripts, the installed modules (e.g., py_install("pandas", "mmstat4.hu.data")) are not found. I believe this happens because reticulate is using r-reticulate instead of mmstat4.hu.data. How can I force reticulate to use my virtual environment?

Thanks Sigbert

> library("reticulate")
> py_config()
python:         /home/sk/.virtualenvs/r-reticulate/bin/python
libpython:      /usr/lib/python3.10/config-3.10-x86_64-linux-gnu/libpython3.10.so
pythonhome:     /home/sk/.virtualenvs/r-reticulate:/home/sk/.virtualenvs/r-reticulate
version:        3.10.12 (main, Mar 22 2024, 16:50:05) [GCC 11.4.0]
numpy:          /home/sk/.virtualenvs/r-reticulate/lib/python3.10/site-packages/numpy
numpy_version:  2.0.0
> virtualenv_list()
[1] "mmstat4.hu.data" "r-reticulate"   
> use_virtualenv("mmstat4.hu.data", required=TRUE)
> py_config()
python:         /home/sk/.virtualenvs/r-reticulate/bin/python
libpython:      /usr/lib/python3.10/config-3.10-x86_64-linux-gnu/libpython3.10.so
pythonhome:     /home/sk/.virtualenvs/r-reticulate:/home/sk/.virtualenvs/r-reticulate
version:        3.10.12 (main, Mar 22 2024, 16:50:05) [GCC 11.4.0]
numpy:          /home/sk/.virtualenvs/r-reticulate/lib/python3.10/site-packages/numpy
numpy_version:  2.0.0
> virtualenv_list()
[1] "mmstat4.hu.data" "r-reticulate"   

Hi,

not the prettiest way but this works for me

Sys.setenv(RETICULATE_PYTHON_ENV="sigbert")
library(reticulate)
py_config()
virtualenv_list()

output

python:         -.virtualenvs/sigbert/Scripts/python.exe
libpython:      -/Python311/python311.dll
pythonhome:     -/.virtualenvs/sigbert
version:        3.11.7 (tags/v3.11.7:fa7a6f2, Dec  4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)]
Architecture:   64bit
numpy:          -/.virtualenvs/sigbert/Lib/site-packages/numpy
numpy_version:  2.0.0

where sigbert is a non default environment now set and i can use it.

If it needs to be loaded before the script then add a .Renviron file with

RETICULATE_PYTHON_ENV="sigbert"

in your working folder. Hope it helps.

1 Like

There are many ways you can influence which Python reticulate discovers.

Please take a look at the "Order of Discovery" on this page: Python Version Configuration • reticulate.

Hi,

I have found a solution that works for me. I changed the virtual environment on the command line, but of course I run my Python script through the RStudio editor. If I go to Tools -> Global Options -> Python and change to my virtual environment, everything works fine. I'm not sure what the default is when, as in my case, no project is open.

Sigbert

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.