简体   繁体   English

使用 reticulate 在 RStudio 和 shiny 应用程序中运行 python 脚本时出错

[英]Error running python script in RStudio and shiny app using reticulate

I have a python script in which I have defined a function using the "adtk" package.我有一个 python 脚本,其中我使用“adtk”package 定义了一个 function。 Using reticulate, I call my function from R and apply it on some data.使用网状,我从 R 调用我的 function 并将其应用于一些数据。 This totally works with no error.这完全没有错误。 However, when I try to do the exact same thing but in a shiny app, it gives me the following error: module adtk has no attribute transformer !但是,当我尝试在 shiny 应用程序中做完全相同的事情时,它给了我以下错误:模块 adtk 没有属性转换器

RStudio code piece: RStudio代码片:

library(reticulate)
use_condaenv('my_conda_env')
source_python("my_python_script.py")

Python script: Python 脚本:

import adtk

def my_func():
  adtk.transformer.DoubleRollingAggregate()
  ...

It sounds like running your code locally could be unintentionally using your system Python (and its adtk package) rather than using your conda environment, which may be missing adtk and/or some of its modules.听起来在本地运行您的代码可能会无意中使用您的系统 Python (及其 adtk 包),而不是使用您的 conda 环境,它可能缺少 adtk 和/或其某些模块。

When you run library(reticulate) , the reticulate package will try to initialize its best-guess version of Python, which may not be the version that you intend to use.当您运行library(reticulate)时,网状 package 将尝试初始化其最佳猜测版本的 Python,这可能不是您打算使用的版本。 Since Python has already been initialized for your session, running use_condaenv('my_conda_env') afterwards will likely be ignored.由于 Python 已经为您的 session 初始化,之后运行use_condaenv('my_conda_env')可能会被忽略。 (You can test this by trying to run use_condaenv('my_conda_env', required = TRUE) instead to see if you get an error similar to: The requested version of Python (<conda env python path>) cannot be used, as another version of Python (<system python path>) has already been initialized. ) (您可以通过尝试运行use_condaenv('my_conda_env', required = TRUE)来测试它,而不是查看是否收到类似于以下内容的错误: The requested version of Python (<conda env python path>) cannot be used, as another version of Python (<system python path>) has already been initialized.

To force your code to run in your conda environment, restart your R session and run reticulate::use_condaenv('my_conda_env', required = TRUE) .要强制您的代码在您的 conda 环境中运行,请重新启动 R session 并运行reticulate::use_condaenv('my_conda_env', required = TRUE) Confirm that the environment is being used by running reticulate::py_config() .通过运行reticulate::py_config()确认正在使用环境。

You can double check that the correct version of adtk is installed in your conda env by viewing the installed packages .您可以通过查看已安装的软件包来仔细检查您的 conda 环境中是否安装了正确版本的 adtk。 Finally, make sure that conda is supported on the system that you're running your Shiny app ( conda isn't currently supported on shinyapps.io , for example, but you could use virtualenv instead.)最后,确保在您运行 Shiny 应用程序的系统上支持 conda(例如, 在 shinyapps.io 目前不支持 conda ,但您可以改用 virtualenv。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM