shinyshinyappsreticulate

shinyapps says Python package not installed after just installing it


I am having trouble deploying to shinyapps.io a Shiny app using reticulate and sklearn. It worked in the past which is strange.

My showLogs show that the sklearn was installed OK, but then it fails. See bold bits.

2022-12-01T05:35:31.072753+00:00 shinyapps[6354935]: Building wheels for collected packages: sklearn 2022-12-01T05:35:31.073424+00:00 shinyapps[6354935]: Building wheel for sklearn (setup.py): started 2022-12-01T05:35:31.376650+00:00 shinyapps[6354935]: Building wheel for sklearn (setup.py): finished with status 'done' 2022-12-01T05:35:31.377240+00:00 shinyapps[6354935]: Created wheel for sklearn: filename=sklearn-0.0.post1-py3-none-any.whl size=2935 sha256=afa8f4d2a5d1eb8c1108cc46573eee0243edf28ccc4daa14e1653f95f374ef73 2022-12-01T05:35:31.377410+00:00 shinyapps[6354935]: Stored in directory: /home/shiny/.cache/pip/wheels/1c/2f/26/476423e3abcbdc095c9061b4a385339f4d5c4952c036ef8262 2022-12-01T05:35:31.379641+00:00 shinyapps[6354935]: Successfully built sklearn 2022-12-01T05:35:31.474083+00:00 shinyapps[6354935]: Installing collected packages: sklearn, pytz, zope.interface, six, pyparsing, pillow, kiwisolver, fonttools, cycler, contourpy, python-dateutil, packaging, datetime, pandas, matplotlib, wordcloud 2022-12-01T05:35:42.219143+00:00 shinyapps[6354935]: Successfully installed contourpy-1.0.6 cycler-0.11.0 datetime-4.7 fonttools-4.38.0 kiwisolver-1.4.4 matplotlib-3.6.2 packaging-21.3 pandas-1.5.2 pillow-9.3.0 pyparsing-3.0.9 python-dateutil-2.8.2 pytz-2022.6 six-1.16.0 sklearn-0.0.post1 wordcloud-1.8.2.2 zope.interface-5.5.2 2022-12-01T05:35:48.964346+00:00 shinyapps[6354935]: Matplotlib is building the font cache; this may take a moment. 2022-12-01T05:35:51.393586+00:00 shinyapps[6354935]: Error in value[3L] : 2022-12-01T05:35:51.393620+00:00 shinyapps[6354935]: ModuleNotFoundError: No module named 'sklearn' 2022-12-01T05:35:51.393625+00:00 shinyapps[6354935]: Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> 2022-12-01T05:35:51.393629+00:00 shinyapps[6354935]: Execution halted

I am creating a virtualenv when it sees user Shiny. I was using this guidance.

if (Sys.info()[['user']] == 'shiny'){
  # When running on shinyapps.io, create a virtualenv
  envs<-reticulate::virtualenv_list()
  if(!'venv_shiny_app' %in% envs)
  {
    reticulate::virtualenv_create(envname = 'venv_shiny_app',
                                  python = 'python3')
    reticulate::virtualenv_install('venv_shiny_app',
                                   packages = c('matplotlib',
                                                'pandas',
                                                'numpy',
                                                'wordcloud',
                                                'sklearn',
                                                'datetime'))
  }
  # https://github.com/ranikay/shiny-reticulate-app
  # Set environment BEFORE this
  reticulate::use_virtualenv('venv_shiny_app', required = TRUE)

} else {
  print(paste0("User: ", Sys.info()[['user']]))
}

When I call from global.R the source:

source_python("code/dash-babbleapp-fun.py") 

It is going to a script that has this code.

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS as sk_stopwords

Is this the problem that a subset of sklearn is not being seen?


Solution

  • Do you have scikit-learn in your requirements file/run pip install scikit-learn?

    The package was renamed from sklearn to scikit-learn so that may be the reason why you're now unable to import sklearn (see https://github.com/scikit-learn/scikit-learn/issues/8215). This brownout change started on December 1st, so it is probably no coincidence.