Directory "" is not a Python virtualenv - Python virtualenv not detected in Shiny

Hello,
I am trying to create a shiny app based on shinyapps.io that uses a virtualenv with python3 but I am currently having trouble with shinyapps.io recognising the virtual environment.

I have followed this tutorial to build my app and it works locally: I can access the virtual environment (and when I use reticulate::py_config() it tells me its running on the virtualenv).

Nevertheless, when I try to run it with shinyapps.io I always run into the same problem:

Error in value[[3L]](cond) : 
  Directory /cloud/project/python35_env/ is not a Python virtualenv
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>

My code is:

library(tidyverse)
library(lubridate)
library(shiny)
library(leaflet)

reticulate::use_virtualenv("/cloud/project/python35_env/", required = TRUE)
reticulate::source_python("getdatafunction.py")

I have created the virtual environment on the console as the tutorial suggests (and I have tried creating other virtual environments at the same time the server was called) yet my problem is always the same. The code I wrote in the console is:

reticulate::virtualenv_create("python35_env", python = "usr/bin/python3")
reticulate::virtualenv_install("python35_env", c("pandas", "numpy", "requests", "requests_aws4auth", "elasticsearch_dsl", "elasticsearch"))

I don't know what else I could try. I am currently running it on RStudio Cloud as you can appreciate, but when I tried on the local console the error was still the same. It is always failing to recognise the virtual environment as what it is.

The architecture of the project is:
Cloud > project. In project we have stored:

  • app.R
  • getdatafunction.py
  • .Rprofile
  • rsconnect
  • python35_env

In .Rprofile I have this code:

# .RProfile
if (!Sys.info()[['sysname']] == 'Darwin'){
  Sys.setenv(RETICULATE_PYTHON = '/cloud/project/python35_env')
}

Any idea about how I could solve this?

This path doesn't exist on the shiny server (shinyapps.io), have in mind that RStudio Cloud and shinyapps.io are separate services with individual environments, and when you deploy an app is going to be running on a different system so everything has to be created within the script itself.

1 Like

Hello andresrcs, thank you for your quick response!

I understand what you said and tried to do it locally. Followed the same procedure as before. Locally I can't even get past the reticulate::virtualenv_install line, even though I can use the virtualenv, it does not detect it when I try to install packages on it:

On the same note, when I upload the file to shiny it prints the same error:

Error in value[[3L]](cond) : 
  Directory C:/Users/homy146/Documents/.virtualenvs/python3_env is not a Python virtualenv
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>

My .Rprofile is:

# .RProfile
Sys.setenv(RETICULATE_PYTHON = "C:/Users/homy146/Documents/.virtualenvs/python3_env/Scripts/python")

Exactly the same issue, this path only exist in your local machine, it doesn't exist in the shinyapps.io servers so it is never going to work, you have to create the virtual environment within the script of the shiny app and use a relative path instead of an absolute one (like you are doing so far).

It worked! Thank you for your help!
Just a recap for anyone that is going through the same problem:

  • I used the reticulate development version pointed in the tutorial I mentioned before.
  • I wrote this in my app.R:
reticulate::virtualenv_create("python35_env", python = "python3")
reticulate::virtualenv_install("python35_env", packages = c("pandas", "requests", "numpy", "elasticsearch", "elasticsearch_dsl", "requests_aws4auth"))
reticulate::use_virtualenv("python35_env", required = TRUE)
reticulate::source_python("getdatafunction.py")
  • I wrote this in my .Rprofile:
# .RProfile
Sys.setenv(RETICULATE_PYTHON = "~/.virtualenvs/python35_env/bin/python")

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