2

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()
  ...

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Rani for the response.You are indeed right. But, I have tried your suggestion and still receives the same error. Actually, importing the transformer sub-package and the DoubleRollingAggregate in the python script as import adtk.transormer.DoubleRollingAggregate, works well! The error was for when I only did import adtk in the python script. In the latter case, I added import("adtk.transformer") in the app script in R, and the error was gone. I think importing only adtk doesn't make its sub-packages available in the global namespace or in the sys.modules!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.