R
keras
tensorflow
error handling
package installation

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.

Practice ML system design

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

r
library(keras)
# Error: Python module tensorflow.contrib.keras.python.keras was not found

This happens because:

  1. Your R keras package is an old version that references the TF 1.x path
  2. TensorFlow 2.x is installed in the Python environment, which has no contrib module
  3. Or TensorFlow is not installed at all in the Python environment R is using

Fix 1: Install the Latest R keras Package

r
1# Update R packages
2install.packages("keras")
3install.packages("tensorflow")
4
5# Install TensorFlow and Keras Python dependencies
6library(keras)
7install_keras()  # This installs TF 2.x + Keras in a conda environment

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

r
1library(tensorflow)
2
3# Install TensorFlow 2.x
4install_tensorflow(version = "2.15")
5
6# Verify installation
7tf$constant("Hello TensorFlow")
8# tf.Tensor(b'Hello TensorFlow', shape=(), dtype=string)
9
10# Check version
11cat("TensorFlow version:", tf$`__version__`, "\n")

Fix 3: Configure the Python Environment

R uses the reticulate package to call Python. Make sure it is pointing to the correct Python environment:

r
1library(reticulate)
2
3# Check which Python R is using
4py_config()
5# python: /Users/you/miniconda3/envs/r-tensorflow/bin/python
6# libpython: ...
7# numpy: ...
8# tensorflow: 2.15.0
9
10# If wrong, set the correct environment
11use_condaenv("r-tensorflow")
12# or
13use_virtualenv("~/.virtualenvs/r-tensorflow")
14# or specify Python directly
15use_python("/usr/local/bin/python3")

Add this to your .Rprofile to make it persistent:

r
# ~/.Rprofile
Sys.setenv(RETICULATE_PYTHON = "/path/to/python3")

Fix 4: Fresh Install from Scratch

If the environment is corrupted, start over:

r
1library(reticulate)
2
3# Remove old conda environment
4conda_remove("r-tensorflow")
5
6# Reinstall
7library(keras)
8install_keras(method = "conda", tensorflow = "2.15")
9
10# Verify
11library(keras)
12model <- keras_model_sequential()
13model %>%
14    layer_dense(units = 64, activation = "relu", input_shape = c(10)) %>%
15    layer_dense(units = 1)
16summary(model)

Fix 5: Using pip Instead of conda

r
1library(keras)
2
3# Install using pip instead of conda
4install_keras(method = "virtualenv", tensorflow = "2.15")
5
6# Or install TensorFlow separately
7library(tensorflow)
8install_tensorflow(method = "virtualenv")

Verifying the Installation

r
1library(keras)
2library(tensorflow)
3
4# Check versions
5cat("TF version:", tf$`__version__`, "\n")
6# TF version: 2.15.0
7
8# Test a simple model
9model <- keras_model_sequential() %>%
10    layer_dense(units = 32, activation = "relu", input_shape = c(784)) %>%
11    layer_dense(units = 10, activation = "softmax")
12
13model %>% compile(
14    optimizer = "adam",
15    loss = "categorical_crossentropy",
16    metrics = c("accuracy")
17)
18
19cat("Model compiled successfully\n")

TensorFlow Version Compatibility

R keras versionTensorFlow VersionKeras Path
< 2.2.0TF 1.xtensorflow.contrib.keras
2.2.0 - 2.8.0TF 2.0 - 2.10tensorflow.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

r
1# Old code (TF 1.x era)
2# from tensorflow.contrib.keras.python.keras import models
3# model <- keras::keras_model_sequential()
4
5# New code (TF 2.x): same R API, different Python backend
6library(keras)
7model <- keras_model_sequential() %>%
8    layer_dense(units = 128, activation = "relu", input_shape = c(784)) %>%
9    layer_dropout(rate = 0.3) %>%
10    layer_dense(units = 10, activation = "softmax")

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

r
1library(reticulate)
2
3# Check if tensorflow is importable
4py_module_available("tensorflow")
5# TRUE or FALSE
6
7# Check if keras is importable
8py_module_available("keras")
9
10# Try importing directly
11tf <- import("tensorflow")
12cat("TF version:", tf$`__version__`, "\n")
13
14# Check keras location
15keras_module <- import("tensorflow.keras")
16cat("Keras imported from tensorflow.keras\n")

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") or remotes::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") or install_tensorflow(version = "gpu").
  • Apple Silicon (M1/M2): On macOS ARM, use install_keras(tensorflow = "2.15") which includes Metal support via tensorflow-metal. Older TF versions may not have ARM builds.

Summary

  • The error occurs because the R keras package references the removed tensorflow.contrib.keras path
  • Update the R keras and tensorflow packages, then run install_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.contrib was removed in TF 2.0. All modern Keras access is through tensorflow.keras
  • Set RETICULATE_PYTHON in .Rprofile to ensure R always finds the right Python environment

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.