Correct python version/virtual environment with `reticulate` in RStudio-connect

I'm trying to make reticulate work, but am not able to use the correct venv or python version.

  1. I'm running RStudio-connect on an AWS EC2 instance--have had no problems with this for Rmd and shiny apps.
  2. Installed python 3.7.4 from source, as suggested, into /opt/Python/3.7.4/bin/python3.
  3. to the rstudio-connect.gcfg file added:
[Python]
Enabled = true
Executable = /opt/Python/3.7.4/bin/python3

and restarted rstudio-connect.

  1. On my local machine (a MacBook pro) I have--following suggested best practices--set up a virtual environment in the directory for my project. Within this I have installed pandas, numpy and boto3.

  2. At the top of the app I've loaded the necessary packages (shiny, reticulate), then referenced my virtual environment: use_virtualenv("./my_venv", required=TRUE).

When I publish this to rs-connect, I get the error message:

ERROR: The requested version of Python
('/opt/rstudio-connect/mnt/app/venv/bin/python') cannot be used, as
another version of Python ('/usr/bin/python3') has already been
initialized. Please restart the R session if you need to attach
reticulate to a different version of Python.

Can someone tell me how else I can "attach reticulate to a different version of Python"? I've already called Sys.setenv(RETICULATE_PYTHON = "/opt/Python/3.7.4/bin/python3") before loading reticulate.

Also, when I start R on the command line of the RStudio-connect server and enter reticulate::py_discover_config() I get:

python:         /usr/bin/python3
libpython:      /usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6.so
pythonhome:     /usr:/usr
version:        3.6.8 (default, Jan 14 2019, 11:02:34)  [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
numpy:          /usr/local/lib/python3.6/dist-packages/numpy
numpy_version:  1.17.2

python versions found:
 /usr/bin/python
 /usr/bin/python3

which would seem to indicate that the executable being specified in the .gcfg file is being ignored.

Thank for any pointers--looking forward to getting this working.

1 Like

Thank you for using RStudio connect.

Looks like your rstudio-connect.gcfg configuration and most of your setup is correct so lets try a couple of thing figure it out.

Just so I know, are you trying to deploy a reticulated shiny app or an Rmd that calls Python?

I think whats happening is that this line: use_virtualenv("./my_venv", required=TRUE) is in one of your R scripts and since RStudio connect already handles the creation of the Python virutalenv your script is always trying to overwrite that.

To fix this and still being able to reference your local environment in your laptop remove that line from your R scripts and instead change the .Rprofile file in your project to have this line: Sys.setenv(RETICULATE_PYTHON = "./my_venv/bin/python3").

You can also do this in your global ~/.Rprofile but in that case use the full path to the python binary: Sys.setenv(RETICULATE_PYTHON = "/Users/daniel/full/path/to/my_venv/bin/python3")

After that you have to restart your local R Session and call reticulate::py_discover_config() to verify that is indeed using the Python from your local virtual env. This is important since rsconnect locally needs to know what environment dependencies to send to the server.

After you have made those changes try deploying the app again to rstudio connect. Maybe also try deploying it with a new name so the whole process is restarted but it should not needed.

On the server its fine for an R console to not being able to find the correct Python version since they are not linked by default. RStudio connect is the one that uses the Python configuration on the rstudio-connect.gcfg file to create a new virutalenv and point the R session to those.

3 Likes

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