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.
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:
- '
pipinstalled the package into a different interpreter thanpython' - 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.
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:
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:
or:
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:
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 tfin a terminal before debugging VS Code. - Use
python -m pipso package installation targets the same interpreter you run. - Select the project virtual environment explicitly inside VS Code.
- Prefer
tf.kerasorfrom tensorflow import kerasand avoid mixing import styles without reason. - Check for local files that shadow
tensorfloworkeras.

