5

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.
4

1 回答 1

8

要使用 Python 3 将应用程序部署到 shinyapps.io reticulate,您的代码应该创建一个 Python 3 虚拟环境并在其中安装任何所需的包:

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

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

然后,不使用该use_python()函数,只需指向reticulate您创建的 Python 3 虚拟环境:

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

有关使用 Python 3 将 Shiny 应用程序部署到 shinyapps.io 的更完整教程reticulate,请查看此分步示例

注意:直到几个月前,从 Python 3reticulate调用virtualenv仍默认创建 Python 2 虚拟环境。但是,此问题已在2019 年 10 月 8 日的开发版本中得到修复。reticulate

reticulate您可以使用 R 包安装带有修复程序的特定版本remotes

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

或使用任何reticulate>= v1.13.0-9001,您将能够在 shinyapps.io 上创建 Python 3 虚拟环境。

于 2019-12-01T18:02:30.077 回答