ModuleNotFoundError: No module named 'keras' (Python) — how to fix
Traceback (most recent call last):
File "train.py", line 1, in <module>
import keras
ModuleNotFoundError: No module named 'keras'
ModuleNotFoundError: No module named 'keras.preprocessing.text'
Quick fix: Install into the same interpreter: python -m pip install keras tensorflow (or from tensorflow import keras). For old Keras 2 code on TensorFlow 2.16+, install tf-keras and set TF_USE_LEGACY_KERAS=1, or update imports to the Keras 3 API.
What it means
Python searched the sys.path of the interpreter that is running your code and found no keras package (or no submodule with that name). Either Keras isn't installed there, or the import path you use doesn't exist in the installed version.
Check which interpreter runs and whether it has Keras:
python -c "import sys; print(sys.executable)"
python -m pip show keras tensorflow
Fix 1: install Keras into the right environment
Use python -m pip so pip installs for the same interpreter that runs your code:
python -m pip install keras
Keras 3 needs a backend. The usual choice is TensorFlow:
python -m pip install tensorflow # also installs keras
TensorFlow 2.x ships Keras, so from tensorflow import keras works whenever TensorFlow is installed.
In a virtual environment, activate it first (source .venv/bin/activate, or .venv\Scripts\activate on Windows). With conda: conda activate myenv, then install.
Fix 2: Jupyter / VS Code uses a different kernel
If pip install succeeded but the notebook still fails, the kernel is another Python. Install from inside the notebook:
%pip install keras tensorflow
Then restart the kernel. In VS Code, pick the interpreter of your venv ("Python: Select Interpreter").
Fix 3: old import paths removed in Keras 3
TensorFlow 2.16 and later use Keras 3, which removed several Keras 2 modules. Errors like these come from old tutorials:
No module named 'keras.preprocessing.text'
No module named 'keras.layers.advanced_activations'
No module named 'keras.wrappers'
Option A: update the code. For example, text preprocessing moved to layers:
from keras.layers import TextVectorization
from keras.utils import pad_sequences
Option B: keep Keras 2 behaviour via the tf-keras package:
python -m pip install tf-keras
export TF_USE_LEGACY_KERAS=1 # set before importing tensorflow
Then use tf.keras in your code. Or pin an older TensorFlow that still bundles Keras 2 (tensorflow<2.16), if your Python version is supported by it.
Fix 4: TensorFlow won't install, so Keras can't import
pip install tensorflow fails with "No matching distribution found" when your Python version is newer than the latest TensorFlow wheels, or on 32-bit Python. Check python --version against the versions listed on TensorFlow's install page, and create a venv with a supported Python. Keras 3 can also run on the JAX or PyTorch backends:
python -m pip install keras torch
export KERAS_BACKEND=torch
Fix 5: a local file shadows the package
A file named keras.py (or a folder named keras) in your project directory breaks the import. Rename it and delete any __pycache__ next to it.
Last updated 2026-12-01.