keras
import error
installation issue
python
machine learning

Cannot import keras after installation

Master System Design with Codemia

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

Introduction

Import errors after installing Keras usually come from environment mismatch, package version conflicts, or using the wrong import path for your TensorFlow stack. The fix is rarely a single reinstall command. A stable solution starts with verifying which Python interpreter is active, then aligning TensorFlow and Keras versions intentionally.

Step 1: Confirm Interpreter and Environment

Most cases fail because package was installed in one environment while code runs in another.

bash
python -V
which python
python -m pip -V

These commands should all point to the same environment path. If they do not, activate the correct virtual environment first.

For venv:

bash
source .venv/bin/activate

For conda:

bash
conda activate ml-env

Step 2: Inspect Installed Packages

Check what is actually installed.

bash
python -m pip show tensorflow keras
python -m pip list | grep -E "tensorflow|keras"

If packages are missing or versions are inconsistent, uninstall and reinstall in the active environment.

bash
python -m pip uninstall -y keras tensorflow tensorflow-cpu
python -m pip install --upgrade pip
python -m pip install tensorflow

For most modern setups, installing TensorFlow is enough because Keras is bundled as tf.keras.

Step 3: Use the Correct Import Path

In many projects, this is the key fix:

python
from tensorflow import keras

Instead of:

python
import keras

Standalone keras package and TensorFlow-integrated Keras can diverge by version and behavior. Prefer one stack consistently across your codebase.

Step 4: Validate with a Minimal Smoke Test

Run a small script in the same shell where you execute your project.

python
1import tensorflow as tf
2from tensorflow import keras
3
4print("TF version:", tf.__version__)
5print("Keras version:", keras.__version__)
6
7model = keras.Sequential([
8    keras.layers.Dense(8, activation="relu", input_shape=(4,)),
9    keras.layers.Dense(1)
10])
11
12model.compile(optimizer="adam", loss="mse")
13print("Keras import and compile succeeded")

If this works, your runtime environment is functional and issues likely come from project-specific tooling.

Step 5: Check IDE and Notebook Kernel Settings

Your terminal environment can be correct while IDE uses a different interpreter. In VS Code, PyCharm, and Jupyter, confirm selected interpreter or kernel points to the same environment where packages were installed.

For Jupyter, install kernel from active environment:

bash
python -m ipykernel install --user --name ml-env --display-name "Python (ml-env)"

Then select that kernel in notebook UI.

Step 6: Handle Common Platform Issues

Typical platform-related causes:

  • old Python version incompatible with selected TensorFlow build
  • architecture mismatch, such as wrong wheel type
  • stale package cache causing partial installs

Useful cleanup command:

bash
python -m pip cache purge

Then reinstall from scratch in a fresh environment.

Step 7: Avoid Mixed Package Managers

Mixing conda and pip carelessly can break dependency resolution. If using conda, either manage everything there or keep pip installs minimal and explicit after conda environment creation.

A clean pattern:

  1. create new env
  2. activate env
  3. upgrade pip
  4. install tensorflow
  5. run smoke test

This is often faster than debugging a polluted environment.

CI and Deployment Considerations

If imports work locally but fail in CI or production, pin versions in lock files and use reproducible build steps. Include an import smoke test in pipeline so dependency drift is detected early.

Example CI command:

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

Common Pitfalls

  • Installing package with one Python interpreter and running code with another.
  • Importing standalone keras while environment is built for tf.keras.
  • Ignoring IDE interpreter mismatch and debugging in the wrong environment.
  • Mixing conda and pip installs without a dependency plan.
  • Reinstalling repeatedly without first verifying active environment paths.

Summary

  • Most Keras import issues are environment alignment problems, not broken code.
  • Verify interpreter, pip target, and package versions before reinstalling.
  • Prefer from tensorflow import keras for modern TensorFlow workflows.
  • Use minimal smoke tests to confirm runtime health.
  • Keep environments reproducible to avoid repeated import failures.

Course illustration
Course illustration

All Rights Reserved.