Keras
TensorFlow
Anaconda
Machine Learning
Deep Learning

How to make Keras use Tensorflow backend in Anaconda?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Inside an Anaconda environment, Keras uses the backend that is available and configured for that environment. In older standalone Keras setups, you selected the backend through a config file. In modern TensorFlow-based workflows, the simpler answer is usually to install TensorFlow in the same conda environment and either import from tensorflow.keras or explicitly set the Keras backend to TensorFlow.

The Cleanest Setup in Anaconda

Start by creating a dedicated environment so the Keras and TensorFlow versions stay aligned:

bash
conda create -n keras-tf python=3.11
conda activate keras-tf
pip install tensorflow keras

Using a dedicated environment matters because Keras backend issues are often version-alignment problems in disguise. If TensorFlow is installed in one environment and Keras in another, or if your shell activates the wrong interpreter, backend selection appears broken even though the packages simply do not match.

Modern Approach: Use TensorFlow Directly

In many current projects, the easiest path is to skip standalone backend management entirely and use tensorflow.keras:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(8, activation="relu"),
6    tf.keras.layers.Dense(1, activation="sigmoid"),
7])
8
9model.compile(optimizer="adam", loss="binary_crossentropy")
10print(tf.__version__)

If this imports successfully, Keras is already running on TensorFlow. There is no separate backend file to manage in that setup.

Explicit Backend Selection in Standalone Keras

If you are using standalone Keras with backend selection support, set the backend before importing Keras code:

bash
export KERAS_BACKEND=tensorflow
python train.py

You can verify the result with:

python
1import keras
2from keras import backend
3
4print(backend.backend())

That should print tensorflow.

This environment-variable approach is preferable to editing package internals because it is explicit, reproducible, and easy to automate in shell scripts or notebook launchers.

Legacy keras.json Configuration

Older Keras installations used a config file in the user home directory, often under ~/.keras/keras.json. A typical backend section looked like this:

json
{
  "backend": "tensorflow"
}

If you are maintaining an older environment, that file can still explain why Keras chooses an unexpected backend. But for most current Anaconda setups, the environment variable or tensorflow.keras import path is the more relevant mechanism.

Verifying the Active Environment

When backend selection does not behave as expected, verify that Python, pip, Keras, and TensorFlow are all coming from the same conda environment:

bash
1which python
2python -m pip show tensorflow
3python -m pip show keras
4python -c "import sys; print(sys.executable)"

If those commands point to different interpreters or prefixes, fix the environment first. Backend troubleshooting is wasted effort if the shell is simply running the wrong Python.

GPU and CPU Notes

Using TensorFlow as the backend does not automatically mean GPU acceleration is working. That is a separate issue involving TensorFlow build compatibility, drivers, and platform support. Backend selection only determines which deep-learning engine Keras delegates to.

So if Keras runs but training is slow, the backend may already be correct and the real issue may be hardware acceleration.

Common Pitfalls

The most common pitfall is mixing keras and tensorflow.keras imports in the same project. That can create subtle incompatibilities and make it harder to reason about which backend path is active.

Another pitfall is editing the wrong environment. Developers often change a config file or install TensorFlow, but the notebook kernel or shell session is still attached to another conda environment.

People also assume that installing Anaconda alone selects a backend. It does not. Anaconda only manages environments and packages; the backend still depends on what you installed and how Keras is configured.

Finally, do not keep chasing backend configuration if the real problem is a version mismatch. Compatible package versions matter more than any one config switch.

Summary

  • In Anaconda, install Keras and TensorFlow in the same environment first.
  • The simplest modern option is to use tensorflow.keras, which already runs on TensorFlow.
  • If you use standalone Keras, set KERAS_BACKEND=tensorflow before import.
  • Older environments may still rely on ~/.keras/keras.json.
  • Always confirm that Python, pip, Keras, and TensorFlow all come from the same conda environment.

Course illustration
Course illustration

All Rights Reserved.