TensorFlow.keras namespace not recognized by PyLance in Visual Studio Code
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
When PyLance does not recognize tensorflow.keras, the code may still run even though the editor reports missing imports or weak IntelliSense. In most cases the real issue is interpreter selection, environment mismatch, or the fact that TensorFlow exposes part of its API dynamically while PyLance relies on static analysis. The first step is always to verify the runtime environment, not to trust the editor warning blindly.
Start With the Selected Interpreter
Before changing any code, confirm that VS Code is using the same interpreter where TensorFlow is installed.
If those commands work in one terminal but VS Code points at another environment, PyLance will analyze the wrong site-packages directory and complain about imports that are actually valid elsewhere.
In VS Code, use the Python interpreter picker and make sure it targets the same virtual environment or conda environment you use in the terminal.
Prefer an Import Style PyLance Handles Well
A practical import style is:
This often works better in editors than scattering imports across deep submodules too early. It also makes the code's intent clear: you are using the Keras API that ships with TensorFlow.
If the project intentionally uses standalone keras, use that consistently instead of mixing both styles.
Verify the Environment Inside Python
A short diagnostic script helps separate editor problems from runtime problems:
Run that in the same interpreter selected in VS Code. If it succeeds, the Python environment is probably fine and the remaining issue is editor indexing or stale language-server state.
Reload PyLance After Environment Changes
Once the interpreter is correct, reload the editor so PyLance rebuilds its analysis state. Common recovery steps are:
- reload the VS Code window
- restart the Python language server
- close and reopen the workspace after changing interpreters
If the import works at runtime but PyLance still underlines it, this is a strong sign that the issue is with analysis state rather than the code itself.
Useful Workspace Settings
You usually do not need special settings for a normal virtual environment, but making the interpreter path explicit can reduce confusion.
The exact path depends on your platform. The point is to remove ambiguity about which Python installation belongs to the workspace.
diagnosticMode does not fix the import itself, but it can reduce noisy analysis while you stabilize the environment.
Distinguish Runtime Errors From Editor Warnings
TensorFlow exposes a large API surface, and static tools do not always understand every dynamically provided symbol. That means some PyLance complaints are editor limitations rather than proof that the code is wrong.
A good rule is:
- if the import fails at runtime, fix the environment
- if the import works at runtime but only PyLance complains, investigate editor indexing and import style next
That distinction prevents a lot of unnecessary reinstall attempts.
Common Pitfalls
The biggest mistake is checking TensorFlow in one terminal and assuming VS Code uses the same interpreter. Terminal and editor environments drift apart easily.
Another issue is mixing keras and tensorflow.keras casually in the same project. That makes both runtime behavior and editor analysis harder to reason about.
People also chase PyLance warnings before actually running the code. The runtime result is more important than the editor hint.
Finally, do not keep reinstalling packages if the real problem is only stale language-server state. Interpreter selection and reloads should come first.
Summary
- Verify that VS Code uses the same Python interpreter where TensorFlow is installed.
- Prefer clear imports such as
from tensorflow import keraswhen usingtf.keras. - Keep
kerasandtensorflow.kerasusage consistent within one project. - Reload PyLance after interpreter changes so analysis state is rebuilt.
- Distinguish runtime import failures from editor-only false positives.
Related reading
- TensorFlow/Keras Using specific class recall as metric for Sparse Categorical Cross Entropy
- Tensorflow.keras.layers unresolved reference in pycharm
- Tensorflow/models uses COCO 90 class ids although COCO has only 80 categories
- tensorflow.python.framework.errors_impl.NotFoundError Failed to create a directory ; No such file or directory
- Test parameterization in xUnit.net similar to NUnit
- Testing if object is of generic type in C
- tensorflow.python.framework.errors_impl.NotFoundError while creating a custom inception
- tensorflow.python.framework.errors_impl.UnknownError Failed to rename Input/output error

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.