Keras: Error: Python module keras was not found.

Created on 4 Mar 2018  Â·  18Comments  Â·  Source: rstudio/keras

I am going to work with keras on R and I face with the below error:

Error: Python module keras was not found.

Detected Python configuration:

python: C:\PROGRA~3\ANACON~2\python.exe
libpython: C:/PROGRA~3/ANACON~2/python36.dll
pythonhome: C:\PROGRA~3\ANACON~2
version: 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)]
Architecture: 64bit
numpy: C:\PROGRA~3\ANACON~2\lib\site-packages\numpy
numpy_version: 1.13.3
keras: [NOT FOUND]

python versions found:
C:\ProgramData\Anaconda33\envs\r-tensorflow\python.exe
C:\PROGRA~3\ANACON~2\python.exe
C:\Users\navid\AppData\Local\Programs\Python\PYTHON~1\python.exe
C:\Users\navid\AppData\Local\Programs\Python\Python37\python.exe
C:\ProgramData\Anaconda33\python.exe

Most helpful comment

Somehow Keras isn't installed or loadable in your r-tensorflow conda environment. Maybe try from a fresh R session:

library(reticulate)
conda_remove("r-tensorflow")
library(keras)
install_keras()

Then in another fresh R session:

library(keras)
use_condaenv("r-tensorflow")
dataset_mnist()

Then if that fails:

library(reticulate)
py_run_string('import keras')

All 18 comments

It looks like you haven't yet called the install_keras() function?

I have, these are the codes and the error:

install.packages("keras")
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.4/keras_2.1.4.zip'
Content type 'application/zip' length 2098280 bytes (2.0 MB)
downloaded 2.0 MB

package ‘keras’ successfully unpacked and MD5 sums checked

The downloaded binary packages are in
C:\Users\navid\AppData\Local\Temp\RtmpC8VIL2\downloaded_packages

library(keras)
install_keras()
Using r-tensorflow conda environment for TensorFlow installation
Solving environment: ...working... done

All requested packages already installed.

Installation complete.

Restarting R session...

library(keras)
mnist <- dataset_mnist()
Error: ModuleNotFoundError: No module named 'keras'

There's some sort of error loading Keras but I'm not sure what it's source is. What about if you try executing py_run_string('import keras')

py_run_string('import keras')
Error in py_run_string("import keras") :
could not find function "py_run_string"

library(reticulate)
py_run_string('import keras')
Error in py_run_string_impl(code, local, convert) :
ModuleNotFoundError: No module named 'keras'

Somehow Keras isn't installed or loadable in your r-tensorflow conda environment. Maybe try from a fresh R session:

library(reticulate)
conda_remove("r-tensorflow")
library(keras)
install_keras()

Then in another fresh R session:

library(keras)
use_condaenv("r-tensorflow")
dataset_mnist()

Then if that fails:

library(reticulate)
py_run_string('import keras')

after the first part, I closed R, saved workspace, and open a fresh R and then:

library(keras)
use_condaenv("r-tensorflow")
dataset_mnist()
Error: ModuleNotFoundError: No module named 'keras'
library(reticulate)
py_run_string('import keras')
Error in py_run_string_impl(code, local, convert) :
ModuleNotFoundError: No module named 'keras'

Detailed traceback:
File "", line 1, in

Then something else I don't understand is going on in your configuration. If you can successfully install keras and then it subsequently can't be loaded then there could be another misconfiguration or conflict we aren't seeing.

Ok, any way thank you very much for your attention.

This issue has still not been resolved !

I had to run install_keras() after restarting R to remove the same error:

library(keras)
install_keras(method = c("auto", "virtualenv", "conda"), conda = "auto",
              tensorflow = "default", extra_packages = NULL)

In case this doesn't work, another thread was looking at a similar problem:
https://github.com/rstudio/keras/issues/102

I solved this issue by upgrading R (from R 3.5.0 to R 3.5.1).

@cbuzby : do we have to run instal_keras() everytime a new R session is started?

No you don't.

On Thu, Sep 13, 2018 at 8:16 AM Sanjay notifications@github.com wrote:

@cbuzby https://github.com/cbuzby : do we have to run instal_keras()
everytime a new R session is started?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/rstudio/keras/issues/307#issuecomment-420985649, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAGXxyDsojqx-sRX_plm4UnkkFe9f1-1ks5uakyUgaJpZM4SbX8D
.

I had the similar issue today, was doing this on my mac. Had installed anaconda which had base env, the install was for all users and installed in /opt/anaconda/anaconda3.
Every time I was doing install_keras(), it was installing it fine with new env 'r-tensorflow' but when I did py_config(), it wasn't detecting keras.

Also, I noticed that I had to initialize use_python() instead of use_condaenv().
In all the failure attempts I was doing use_python('/opt/anaconda/anaconda3/bin/python') for base env python location.
The successful attempt was when I used use_python('/opt/anaconda/anaconda3/envs/r-tensorflow/bin/python').

Here's the code sequence that worked.

library(keras)
library(reticulate)
use_python('/opt/anaconda/anaconda3/envs/r-tensorflow/bin/python')
py_config()

Output:

python:         /opt/anaconda/anaconda3/envs/r-tensorflow/bin/python
libpython:      /opt/anaconda/anaconda3/envs/r-tensorflow/lib/libpython3.6m.dylib
pythonhome:     /opt/anaconda/anaconda3/envs/r-tensorflow:/opt/anaconda/anaconda3/envs/r-tensorflow
version:        3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29)  [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
numpy:          /opt/anaconda/anaconda3/envs/r-tensorflow/lib/python3.6/site-packages/numpy
numpy_version:  1.15.2
keras:          /opt/anaconda/anaconda3/envs/r-tensorflow/lib/python3.6/site-packages/keras

python versions found: 
 /opt/anaconda/anaconda3/envs/r-tensorflow/bin/python
 /usr/bin/python

Hope this helps!

same error !!! install_keras() successfull ....
reticulate::py_discover_config("keras")
python: D:\ANACON~1\envs\R-TENS~1\python.exe
libpython: D:/ANACON~1/envs/R-TENS~1/python36.dll
pythonhome: D:\ANACON~1\envs\R-TENS~1
version: 3.6.6 | packaged by conda-forge | (default, Jul 26 2018, 11:48:23) [MSC v.1900 64 bit (AMD64)]
Architecture: 64bit
numpy: D:\ANACON~1\envs\R-TENS~1\lib\site-packages\numpy
numpy_version: 1.15.4
keras: D:\ANACON~1\envs\R-TENS~1\lib\site-packages\keras__init__.p

python versions found:
D:\ANACON~1\envs\R-TENS~1\python.exe
D:\ANACON~1\python.exe
D:\Anaconda3\python.exe
D:\Anaconda3\envs\r-tensorflow\python.exe

but when I go
library(keras)
model <- keras_model_sequential()
Error: Python module keras was not found.

Detected Python configuration:

python: D:\Anaconda3\envs\r-tensorflow\python.exe
libpython: D:/Anaconda3/envs/r-tensorflow/python36.dll
pythonhome: D:\ANACON~1\envs\R-TENS~1
version: 3.6.6 | packaged by conda-forge | (default, Jul 26 2018, 11:48:23) [MSC v.1900 64 bit (AMD64)]
Architecture: 64bit
numpy: D:\ANACON~1\envs\R-TENS~1\lib\site-packages\numpy
numpy_version: 1.15.4
keras: D:\ANACON~1\envs\R-TENS~1\lib\site-packages\keras__init__.p

python versions found:
D:\Anaconda3\envs\r-tensorflow\python.exe
D:\ANACON~1\python.exe
D:\Anaconda3\python.exe

Has this problem every been resolved (I notice the thread is still open)? I have tried all of the suggestions above, but it seems that keras cannot be loaded in a conda environment.

Has this problem every been resolved (I notice the thread is still open)? I have tried all of the suggestions above, but it seems that keras cannot be loaded in a conda environment.

I have got the same error today. I tried "install_keras()" and there is a new error for that command.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

saanasum picture saanasum  Â·  4Comments

jingweihuang picture jingweihuang  Â·  6Comments

fderyckel picture fderyckel  Â·  4Comments

faltinl picture faltinl  Â·  6Comments

MaximilianPi picture MaximilianPi  Â·  5Comments