Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

package or namespace load failed for ‘keras’ #1154

Closed
max-poltora opened this issue Dec 24, 2020 · 3 comments
Closed

package or namespace load failed for ‘keras’ #1154

max-poltora opened this issue Dec 24, 2020 · 3 comments
Labels
awaiting response Waiting for issue author to respond

Comments

@max-poltora
Copy link

max-poltora commented Dec 24, 2020

Good afternoon,
I am trying to deploy my shinyApp, which uses keras and imports python modules via reticulate::import function on shynyapps.io. The process seems to run smoothly till the very last moment:

rsconnect::deployApp()
Preparing to deploy application...Update application currently deployed at
https://name.shinyapps.io/app_name/? [Y/n] y
DONE
Uploading bundle for application: 3428026...DONE
Deploying bundle: 4025733 for application: 3428026 ...
Waiting for task: 845012118
  building: Parsing manifest
  building: Building image: 4584803
  building: Installing system dependencies
  building: Fetching packages
  building: Installing packages
  building: Installing files
  building: Pushing image: 4584803
  deploying: Starting instances
  terminating: Stopping old instances
Application successfully deployed to https://name.shinyapps.io/app_name/

But when loaded it gives the following error message:

An error has occurred
The application failed to start: exited unexpectedly with code 1

Error in value[3L] :
package or namespace load failed for ‘keras’:
.onLoad failed in loadNamespace() for 'tensorflow', details:
call: py_module_import(module, convert = convert)
error: ImportError: No module named tools

Detailed traceback:
File "/opt/R/3.6.1/lib/R/library/reticulate/python/rpytools/loader.py", line 24, in _import_hook
level=level
File "/usr/local/lib/python2.7/dist-packages/tensorflow/init.py", line 101, in
from tensorflow_core import *
File "/opt/R/3.6.1/lib/R/library/reticulate/python/rpytools/loader.py", line 24, in _import_hook
level=level
File "/usr/local/lib/python2.7/dist-packages/tensorflow_core/init.py", line 40, in
from tensorflow.python.tools import module_util as _module_util
File "/opt/R/3.6.1/lib/R/library/reticulate/python/rpytools/loader.py", line 24, in _import_hook
level=level
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne ->
Execution halted

The errors message seems to be strange to me, as I do not import any modules called "tools". Here is a peace of my code, that seems to cause execution failure:

library(shiny)
library(shinythemes)
Sys.setenv(RETICULATE_PYTHON = "C:/Users/user/anaconda3/envs/tf")
library(keras)
use_condaenv("tf")

np <- import("numpy", convert=FALSE)
ndi <- import("scipy.ndimage", convert=FALSE)
segment <- import("skimage.segmentation", convert=FALSE)
feature <- import("skimage.feature", convert=FALSE)

model = load_model_hdf5("model_v02122020.h5")

I have found some similar issues (like [this] one (#268)), but suggested solutions do not have an effect in my situation. Could you please kindly help me with this issue?

@max-poltora
Copy link
Author

max-poltora commented Dec 24, 2020

I have slightly modified my code:

reticulate::virtualenv_create(envname = 'tf', python = 'python36')
reticulate::virtualenv_install('tf', c('numpy', 'tensorflow', 'keras'), ignore_installed = FALSE)
reticulate::use_virtualenv(virtualenv = 'tf', required = TRUE)

library(shiny)
library(shinythemes)
library(keras)

np <- import("numpy", convert=FALSE)
ndi <- import("scipy.ndimage", convert=FALSE)
segment <- import("skimage.segmentation", convert=FALSE)
feature <- import("skimage.feature", convert=FALSE)

model = load_model_hdf5("model_v02122020.h5")

But now, the error message is not informative at all. Thus, deployment process runs normally:

rsconnect::deployApp()
Preparing to deploy application...Update application currently deployed at
https://name.shinyapps.io/app_name/? [Y/n] y
DONE
Uploading bundle for application: 3428026...DONE
Deploying bundle: 4025913 for application: 3428026 ...
Waiting for task: 845033169
  building: Parsing manifest
  building: Building image: 4584992
  building: Installing system dependencies
  building: Fetching packages
  building: Installing packages
  building: Installing files
  building: Pushing image: 4584992
  deploying: Starting instances
  rollforward: Activating new instances
  terminating: Stopping old instances
Application successfully deployed to https://name.shinyapps.io/app_name/

The error message:

An error has occurred
The application failed to start: exited unexpectedly with code 1

Error in value[3L] : error in running command
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne ->
Execution halted

@t-kalinowski
Copy link
Member

t-kalinowski commented Jul 27, 2021

Hello,

Please try putting this in your global.R file:

if(!file.exists(reticulate::miniconda_path()))
  reticulate::install_miniconda()
if(!reticulate::py_module_available("tensorflow"))
  keras::install_keras()
keras::install_keras()

@t-kalinowski t-kalinowski added the awaiting response Waiting for issue author to respond label Jul 27, 2021
@github-actions
Copy link

Automatically closed because there has not been a response for 30 days. When you're ready to work on this further, please comment here and the issue will automatically reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response Waiting for issue author to respond
Projects
None yet
Development

No branches or pull requests

2 participants