Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

AI and LLM integration · Step-by-step tutorial

Python cannot import ops from keras

Short answer

keras.ops only exists in Keras 3. The ImportError means the keras package Python loads is a Keras 2 release, either installed on its own or held back by TensorFlow 2.15 or older. Check keras.__version__; if it starts with 2, upgrade Keras, and upgrade TensorFlow with it to a pair the Keras compatibility matrix lists, such as keras 3.15.1 with tensorflow 2.20.

  • Applies to Keras 3.15.1, TensorFlow 2.20, Python 3.11 and later
  • Updated
  • By Maaz Zindani, Senior Software Engineer

keras.ops is a Keras 3 module. When from keras import ops fails with ImportError, the keras package Python loaded is a Keras 2 release. Most often that is because TensorFlow 2.15 or older is installed and holds Keras at version 2, or because code written for Keras 3 is running in an environment that never got the upgrade.

The fix is to get Keras 3 and a matching TensorFlow into the same environment. Keras's own install page lists which pairs belong together, and it is worth copying a pair from there rather than guessing.

Before you start

  • The same Python interpreter that raised the error (activate its virtual environment first)
  • Python 3.11 or newer, which the Keras install page gives as the Keras 3 requirement
  • Permission to change the packages in that environment

Step 1

See which Keras this interpreter loads

Run this with the interpreter that fails. It prints the interpreter path, the Keras version and the TensorFlow version, so you know the major version of Keras and whether TensorFlow is in the way.

Versions as this Python sees them

python -c "import keras, sys; print(sys.executable, keras.__version__); import tensorflow as tf; print(tf.__version__)"

If the path is not the environment you expected, the problem is the environment, not Keras. Notebook kernels and system Python are the usual surprise.

Step 2

If Keras is 2.x and TensorFlow is 2.16 or newer

The TensorFlow 2.16 release notes make Keras 3 the default from that version. A Keras 2 package in that setup was probably pinned or installed on its own. Upgrade Keras; the install page gives the command pip install --upgrade keras.

  1. Upgrade Keras in this environment.
  2. Make sure a backend (TensorFlow, JAX or PyTorch) is installed, since Keras 3 needs one.
  3. Run the check at the end of this page.

Upgrade

python -m pip install --upgrade keras

Step 3

If TensorFlow is 2.15 or older

Upgrading Keras alone makes pip complain that TensorFlow requires an older keras. The Keras compatibility matrix pairs the older TensorFlow releases with Keras 2 only, so the two must move together. The matrix currently lists keras==3.15.1 with tensorflow==2.20.0 for Keras 3.

Move both to a listed pair

python -m pip check
python -m pip install --upgrade "tensorflow==2.20.*" "keras==3.15.1"

On a GPU machine, use the TensorFlow install command for your platform instead of the plain package name.

Step 4

Run pip check, then import ops

pip check should report no broken requirements, and one process should import both packages and the ops module.

One process, both packages

python -m pip check && python -c "import tensorflow as tf, keras; from keras import ops; print(tf.__version__, keras.__version__, ops.ones((2,)))"

Step 5

If it still fails on Keras 3

With keras.__version__ already starting with 3, print keras.__file__. A path inside your own project instead of site-packages means a local file or folder called keras is being imported in place of the package.

Step 6

Pinning so it stays fixed

  • Put keras and tensorflow next to each other in requirements.txt with a pair from the compatibility matrix.
  • Print keras.__version__ in the first cell of a shared notebook, so a Keras 2 runtime shows before the imports fail.
  • Code that must also run on old TensorFlow should stay on tf.keras APIs and avoid keras.ops.

It worked when

  • keras.ops exists only in Keras 3.
  • Upgrade Keras and TensorFlow as a pair from the compatibility matrix.
  • Check the interpreter path before you change anything.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want a second pair of eyes on it?

Send me the error and what changed before it started. I reply within 24 hours.