简体   繁体   中英

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. Using reticulate, I call my function from R and apply it on some data. 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 !

RStudio code piece:

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

Python script:

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.

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. Since Python has already been initialized for your session, running use_condaenv('my_conda_env') afterwards will likely be ignored. (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. )

To force your code to run in your conda environment, restart your R session and run reticulate::use_condaenv('my_conda_env', required = TRUE) . Confirm that the environment is being used by running reticulate::py_config() .

You can double check that the correct version of adtk is installed in your conda env by viewing the installed packages . 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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