Trying to deploy a shiny app that uses python modules

40 Views Asked by At

I'm currently working on a Shiny app that integrates Python code for some of its functionalities. I've used the reticulate package to manage the interaction between R and Python. Locally, everything works as expected, but I'm facing challenges deploying the app to shinyapps.io, particularly with setting up the Python environment correctly on the platform. I've been at this for days and I am losing my mind. I keep getting this error:

Error in stop_no_virtualenv_starter(version = version, python = python) : Suitable Python installation for creating a venv not found.
Requested Python: python3
Please install Python with one of following methods:

- https://github.com/rstudio/python-builds/
- reticulate::install_python(version = '\<version\>')

Here's the context:

My app relies on the openai Python package, among others, specified in a requirements.txt file.

Locally, I create a virtual environment (openai-env). and install the required packages using reticulate functions: virtualenv_create, virtualenv_install, and use_virtualenv. I then source a Python script (Python_Script.py) that utilizes these packages.

The relevant portion of my R code looks something like this:

 `library(reticulate)
  virtualenv_create("openai-env", python = "python3")
  virtualenv_install("openai-env", packages = c("openai"))
  use_virtualenv("openai-env", required = TRUE)
  source_python("Python_Script.py")     `

My .R profile looks like this:

`# This file configures the virtualenv and Python paths differently depending on
     # the environment the app is running in (local vs remote server).

     # Edit this name if desired when starting a new app
     VIRTUALENV_NAME = 'openai-env'


      # ------------------------- Settings (Edit local settings to match your system) ---------     ----------------- #

      if (Sys.info()[['user']] == 'shiny'){
  
     # Running on shinyapps.io
      Sys.setenv(PYTHON_PATH = 'python3')
      Sys.setenv(VIRTUALENV_NAME = VIRTUALENV_NAME) # Installs into default shiny virtualenvs     dir
      Sys.setenv(RETICULATE_PYTHON = paste0('/home/shiny/.virtualenvs/', VIRTUALENV_NAME, '/bin/python'))
  
} else if (Sys.info()[['user']] == 'rstudio-connect'){
  
  # Running on remote server
  Sys.setenv(PYTHON_PATH = '/opt/python/3.7.7/bin/python3')
  Sys.setenv(VIRTUALENV_NAME = paste0(VIRTUALENV_NAME, '/')) # include '/' => installs into rstudio-connect/apps/
  Sys.setenv(RETICULATE_PYTHON = paste0(VIRTUALENV_NAME, '/bin/python'))
  
} else {
  
  # Running locally
  options(shiny.port = 7450)
  Sys.setenv(PYTHON_PATH = 'python3')
  Sys.setenv(VIRTUALENV_NAME = VIRTUALENV_NAME) # exclude '/' => installs into ~/.virtualenvs/
  # RETICULATE_PYTHON is not required locally, RStudio infers it based on the ~/.virtualenvs path
}``
0

There are 0 best solutions below