Cannot import "gym" after installing Keras interface

keras

#1

Hello.
I'm using Tensorflow on R for my research, and I often use "gym" module to check the validity of my reinforcement learning model. There was no problem before installing the Keras interface.

However, I found an error to import Keras and gym modules at the same time, after installing the Keras API.
(When I import just one of these two modules, there was not a problem.)

The following is the detail of the error.

case 1) Import Keras first & import gym after

  library(keras)
  gym <- import("gym")

(result)
Error in import("gym") : could not find function "import"

case 2) Import gym first & import Keras after

  library(reticulate)
  gym <- import("gym")
  library(keras)

(result)

Error: package or namespace load failed for ‘keras’:
...
ImportError: Could not find 'cudart64_80.dll'. TensorFlow requires that this DLL be installed in a directory that is named in your %PATH% environment variable. Download and install CUDA 8.0 from this URL: https://developer.nvidia.com/cuda-toolkit


I'm now using gpu version of Tensorflow, but I found the same error on my Laptop using cpu version of Tensorflow.

If you have any idea, please reply me.
Thank you!


#2

Hi! Welcome!

I don't know why you're seeing an error in the second case (however keras is not your typical R package, and I am not an expert in its internal workings), but the first case is straightforward:

import() is a function in the reticulate package, but here you try to use it before you've loaded reticulate. What happens when you try it this way?

library(keras)
library(reticulate)
gym <- import("gym")

Also, can you confirm that when you run library(keras) by itself, that keras loads without error?


#3

Adding a code "library(reticulate)" in the first case gives the similar result, although my machine already have "gym" module.

library(keras)
library(reticulate)
gym <- import("gym")

(result) ModuleNotFoundError: No module named 'gym'

In addition, before I installed the Keras API, I used tensorflow module only, and the first case
library(tensorflow)
gym <- import("gym")
works well, with no problem.

These kinds of error come up after I installed Keras API.


#4

This happens because keras is installed on the r-tensorflow virtualenv and gym is probably installed in r-reticulate env. You can install gym on the r-tensorflow env by running:

py_install("gym", envname = "r-tensorflow")

or install keras in the r-reticulate env:

py_install("keras", envname = "r-reticulate")


#5

Thank you so much.
I thought this problem is probably happended because of the path of virtualenv, but I did not know what to do.
Your second advice works well and I checked the following message.


library(reticulate)
py_install("keras", envname = "r-reticulate")

gym <- import("gym")
library(keras)

is_keras_available()

(result) -> TRUE


However, your first advice still fails to install gym on virtualenv giving the message:

PackagesNotFoundError: The following packages are not available from current channels:

I also tried 'pip install gym' on anaconda prompt by:
(following the guide from OpenAI website)


(Anaconda prompt)
...> activate r-tensorflow
(r-tensorflow) ...> pip install gym
(r-tensorflow) ...> git clone https://github.com/openai/gym
(r-tensorflow) ...> cd gym
(r-tensorflow) ...> pip install -e.


Although this process successfully installed gym on r-tensorflow virtualenv, I still cannot import gym module on R. [gym <- import("gym")]


#6

Glad it worked!

It's really hard to debug those environments issues. Sometimes the easiest way is to delete the environment and install everything from scratch. :frowning: