R keras package Error Python module tensorflow.contrib.keras.python.keras was not found
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The error Python module tensorflow.contrib.keras.python.keras was not found in R's keras package occurs because the R package is trying to import Keras from a path that only existed in TensorFlow 1.x (tensorflow.contrib.keras). TensorFlow 2.x moved Keras to tensorflow.keras and removed the entire contrib module. The fix is to update both the R keras package and TensorFlow to compatible versions, then configure R to use the correct Python environment.
The Error
This happens because:
- Your R
keraspackage is an old version that references the TF 1.x path - TensorFlow 2.x is installed in the Python environment, which has no
contribmodule - Or TensorFlow is not installed at all in the Python environment R is using
Fix 1: Install the Latest R keras Package
install_keras() creates a dedicated conda environment (r-tensorflow or r-reticulate) with compatible versions of TensorFlow and Keras.
Fix 2: Install TensorFlow in R Explicitly
Fix 3: Configure the Python Environment
R uses the reticulate package to call Python. Make sure it is pointing to the correct Python environment:
Add this to your .Rprofile to make it persistent:
Fix 4: Fresh Install from Scratch
If the environment is corrupted, start over:
Fix 5: Using pip Instead of conda
Verifying the Installation
TensorFlow Version Compatibility
| R keras version | TensorFlow Version | Keras Path |
| < 2.2.0 | TF 1.x | tensorflow.contrib.keras |
| 2.2.0 - 2.8.0 | TF 2.0 - 2.10 | tensorflow.keras |
| 2.9.0+ | TF 2.11+ | tensorflow.keras (or standalone keras) |
The tensorflow.contrib module was completely removed in TensorFlow 2.0 (released October 2019). Any R code or package referencing this path needs updating.
Migrating Old R Code
The R keras API is largely the same between versions. The main changes are in the Python backend path, not the R function names.
Checking Python Module Availability
Common Pitfalls
- Multiple Python installations: R may use a system Python that has no TensorFlow while your conda environment has it. Use
reticulate::py_config()to verify which Python R is actually using. - Mixing conda and pip:
install_keras(method = "conda")creates a conda environment. Installing TensorFlow separately with pip in the system Python creates a second installation that R may not find. - Old R keras package: R packages on CRAN may lag behind. Use
install.packages("keras")orremotes::install_github("rstudio/keras")for the latest version. - GPU support:
install_keras()installs CPU-only TensorFlow by default. For GPU support:install_keras(tensorflow = "gpu")orinstall_tensorflow(version = "gpu"). - Apple Silicon (M1/M2): On macOS ARM, use
install_keras(tensorflow = "2.15")which includes Metal support viatensorflow-metal. Older TF versions may not have ARM builds.
Summary
- The error occurs because the R
keraspackage references the removedtensorflow.contrib.keraspath - Update the R
kerasandtensorflowpackages, then runinstall_keras()to set up a compatible Python environment - Use
reticulate::py_config()to verify R is using the correct Python with TensorFlow 2.x installed tensorflow.contribwas removed in TF 2.0. All modern Keras access is throughtensorflow.keras- Set
RETICULATE_PYTHONin.Rprofileto ensure R always finds the right Python environment
Related reading
- Random number generator differs between tensorflow 1.0.1 and 0.12.1
- Randomly sample from multiple tf.data.Datasets in Tensorflow
- Rank error in tf.nn.dynamic_rnn
- RBM implementation with tensorflow
- Rabbit - Error mnesia_unexpectedly_running
- Rabbit mq - Error while waiting for Mnesia tables
- Re-implementing TF 1.0 sampled_softmax_loss funtion for seq2seq model in to TF 2 Keras model
- Re-initialize variables in Tensorflow
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.