Use python 3 in reticulate on shinyapps.io
Asked Answered
R

1

6

I have some code in Python 3 which I'm running in R through the reticulate library to use in a shiny app. It works fine in my local machine, but when I published in shinyapps.io reticulate is using Python 2 by default.

So far I tried to use use_python function, but I'm not sure about the path:

use_python("/usr/bin/python3", require = TRUE)

The logs give me the error:

2019-02-12T13:44:54.691167+00:00 shinyapps[710102]: Warning: Error in initialize_python: Python shared library '/usr/lib/libpython3.5.so' not found, Python bindings not loaded.
2019-02-12T13:44:54.697101+00:00 shinyapps[710102]:   64: stop
2019-02-12T13:44:54.697103+00:00 shinyapps[710102]:   63: initialize_python
2019-02-12T13:44:54.697104+00:00 shinyapps[710102]:   62: ensure_python_initialized
2019-02-12T13:44:54.697105+00:00 shinyapps[710102]:   61: py_run_file
2019-02-12T13:44:54.697106+00:00 shinyapps[710102]:   60: source_python
2019-02-12T13:44:54.697107+00:00 shinyapps[710102]:   59: server [/srv/connect/apps/str_telefonica/app.R#57]
2019-02-12T13:44:54.697385+00:00 shinyapps[710102]: Error in initialize_python(required_module, use_environment) : 
2019-02-12T13:44:54.697387+00:00 shinyapps[710102]:   Python shared library '/usr/lib/libpython3.5.so' not found, Python bindings not loaded.
Ricoricochet answered 12/2, 2019 at 13:56 Comment(3)
I think this is the standard Linux path: use_python("/opt/python/Python-3.7.1/bin/python3.7")Fawne
Did you ever succeed in getting this to work? I am struggling with it as well..Cubicle
I don't think I didRicoricochet
P
11

To deploy an app to shinyapps.io using reticulate and Python 3, your code should create a Python 3 virtual environment and install any required packages into it:

reticulate::virtualenv_create(envname = 'python3_env', 
                              python = '/usr/bin/python3')

reticulate::virtualenv_install('python3_env', 
                               packages = c('numpy'))  # <- Add other packages here, if needed

Then, instead of using the use_python() function, just point reticulate to the Python 3 virtual environment that you created:

reticulate::use_virtualenv('python3_env', required = T)

For a more complete tutorial on deploying a Shiny app using reticulate with Python 3 to shinyapps.io, check out this step-by-step example.

Note: Until a few months ago, reticulate invoking virtualenv from Python 3 still created a Python 2 virtual environment by default. However, this was fixed in the development version of reticulate as of Oct 8, 2019.

You can install that particular version of reticulate with the fix by using the R package remotes:

remotes::install_github("rstudio/reticulate", force = T, ref = '0a516f571721c1219929b3d3f58139fb9206a3bd')

or use any reticulate >= v1.13.0-9001 and you'll be able to create Python 3 virtual environments on shinyapps.io.

Plunge answered 1/12, 2019 at 18:2 Comment(1)
Thanks for the tutorial. Can I ask why you put virtualenv_create/install into server.R instead of .Rprofile? Still learning shiny. Thanks!Caryl

© 2022 - 2024 — McMap. All rights reserved.