tensorflow
keras
VS Code
import error
Python

Can't import tensorflow.keras in VS Code

Master System Design with Codemia

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

Introduction

When tensorflow.keras cannot be imported in VS Code, the real problem is usually not the import statement itself. In most cases, VS Code is using a different Python interpreter than the one where TensorFlow was installed, or TensorFlow is installed for a Python version it does not support.

The fastest way to fix the issue is to prove that TensorFlow works in a terminal first, then make VS Code use that exact environment. Once the runtime and the editor agree, the import error usually disappears.

Verify TensorFlow Outside VS Code

Before changing editor settings, confirm that TensorFlow is actually installed in the environment you intend to use. Creating a virtual environment is the cleanest option because it isolates project dependencies from global packages.

bash
1python -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip
4python -m pip install tensorflow
5python -c "import tensorflow as tf; print(tf.__version__); print(tf.keras.__name__)"

If the final command prints a TensorFlow version and keras.api._v2.keras or a similar module path, the package is fine. If it fails here, VS Code is not the main issue. Common terminal-side failures include:

  • 'pip installed the package into a different interpreter than python'
  • the current Python version is unsupported by the TensorFlow build you installed
  • installation was interrupted and left a partial environment behind

Using python -m pip instead of plain pip matters because it guarantees that the installer and the interpreter match.

Point VS Code at the Correct Interpreter

Once the terminal import works, open the project folder in VS Code and select the same interpreter. In the Command Palette, run Python: Select Interpreter and choose the .venv interpreter inside the project. If you want to make that choice explicit for the workspace, add a local setting.

json
1{
2  "python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
3  "python.terminal.activateEnvironment": true
4}

After changing the interpreter, reload the window so the Python extension and Pylance rebuild their state. Then open a new integrated terminal inside VS Code and verify that it resolves to the same interpreter:

bash
which python
python -c "import tensorflow as tf; print(tf.__version__)"

If the terminal inside VS Code succeeds but the editor still highlights tensorflow.keras as missing, the issue is often stale language-server state rather than a broken runtime. Reloading the window or restarting the Python language server usually fixes that.

Use an Import Pattern TensorFlow Actually Supports

Modern TensorFlow ships Keras inside the tensorflow package, so the two most common import styles are:

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

or:

python
1from tensorflow import keras
2
3model = keras.Sequential([
4    keras.layers.Input(shape=(4,)),
5    keras.layers.Dense(8, activation="relu"),
6    keras.layers.Dense(1),
7])
8
9print(model.layers)

Both are valid. What often confuses people is mixing the standalone keras package with tensorflow.keras. If your code imports keras directly, but your environment only has TensorFlow installed, the editor may complain for a legitimate reason. Pick one approach for the project and keep it consistent.

Check for Local Naming Conflicts

Python imports can be shadowed by files in the current workspace. If your project contains files or folders named tensorflow.py, keras.py, or even a directory named tensorflow, the import system may load those first and produce misleading errors.

This short check is worth doing:

bash
find . -maxdepth 2 \( -name 'tensorflow.py' -o -name 'keras.py' -o -name 'tensorflow' -o -name 'keras' \)

If the command finds a conflicting filename in your project, rename it and reload VS Code. This is one of the most common reasons an import seems correct but fails in a surprising way.

Common Pitfalls

The biggest pitfall is trusting the editor warning before testing the actual interpreter. If python -c "import tensorflow as tf" fails, the environment is broken and no amount of VS Code tweaking will fix it.

Another common mistake is installing TensorFlow with one command and running the code with another interpreter. This often happens when a system Python, Homebrew Python, Conda environment, and virtual environment all exist on the same machine.

Naming collisions also cause confusion. A local file called keras.py can make Python import your own file instead of the library, which looks like a package failure even though the installation is correct.

Finally, version support matters. Newer Python releases sometimes move faster than TensorFlow wheels. If installation worked only partially or import errors mention binary compatibility, check that your Python version is supported by the TensorFlow package you installed.

Summary

  • Verify import tensorflow as tf in a terminal before debugging VS Code.
  • Use python -m pip so package installation targets the same interpreter you run.
  • Select the project virtual environment explicitly inside VS Code.
  • Prefer tf.keras or from tensorflow import keras and avoid mixing import styles without reason.
  • Check for local files that shadow tensorflow or keras.

Course illustration
Course illustration

All Rights Reserved.