Python
ImportError
TensorFlow
ModuleNotFoundError
AI Development

ImportError No module named 'tensorflow.python'

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

ImportError: No module named 'tensorflow.python' usually means one of two things: TensorFlow is not installed in the Python environment you are actually running, or your code is trying to import TensorFlow internals that should not be imported directly. The fix is usually straightforward once you confirm which of those two cases applies.

Start with the public TensorFlow import

Most application code should import TensorFlow like this:

python
import tensorflow as tf

print(tf.__version__)

You should generally not write imports such as:

python
from tensorflow.python import keras

The tensorflow.python package is an internal implementation detail. Some examples on the internet still use it, but that code is brittle and may break across TensorFlow versions.

If you need Keras, use the public API:

python
1from tensorflow import keras
2
3model = keras.Sequential()
4print(type(model).__name__)

Confirm TensorFlow is installed in the active interpreter

A very common cause is that pip installed TensorFlow into one interpreter while the script, notebook, or IDE is running a different one.

Check the active Python binary:

bash
python -c "import sys; print(sys.executable)"
python -m pip show tensorflow
python -m pip list | grep tensorflow

Using python -m pip is safer than plain pip because it ties the package operation to the interpreter you are about to run.

If TensorFlow is missing, install it in that environment:

bash
python -m pip install tensorflow

Watch for version and environment mismatches

TensorFlow has historically supported only specific Python versions on each release line. If the interpreter version is incompatible, installation may partly fail or the package may not import cleanly.

Create a clean virtual environment when debugging:

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__)"

This removes a lot of noise from system Python installations, stale packages, and IDE-specific environments.

Check for local naming conflicts

Another surprisingly common cause is import shadowing. If your project contains files or folders named tensorflow.py, tensorflow/, or even keras.py, Python may import the local file instead of the real installed package.

You can inspect the import location:

python
import tensorflow as tf

print(tf.__file__)

If that points into your project unexpectedly, rename the conflicting file or directory and remove any stale __pycache__ folders.

Notebook kernels can hide the real interpreter

In Jupyter or VS Code notebooks, the visible terminal environment may not match the kernel actually running your code. A quick in-notebook check helps:

python
import sys

print(sys.executable)

If that path is not the interpreter where TensorFlow was installed, switch kernels or install TensorFlow into the kernel environment instead of the shell environment.

If a dependency imports tensorflow.python

Sometimes your own code is fine, but an older third-party library imports TensorFlow internals. In that case:

  • upgrade the dependency
  • upgrade or downgrade TensorFlow to a compatible version
  • replace the dependency if it relies on unsupported internals

A minimal reproduction in a clean environment helps separate "bad local setup" from "bad dependency import."

Common Pitfalls

The biggest pitfall is trying to fix the error by importing more TensorFlow internals. That usually makes the project more fragile instead of less.

Another issue is using pip install tensorflow in one shell and running the code from another interpreter in an IDE or notebook kernel. The package may be installed correctly, just not where your code is running.

Local file shadowing is easy to miss because the error message often looks like a normal package problem rather than an import path problem.

Finally, avoid assuming old answers still apply to current TensorFlow releases. TensorFlow packaging has changed over time, and internal module paths are especially unstable compared with the public tensorflow API.

Summary

  • Prefer import tensorflow as tf and other public TensorFlow APIs.
  • Do not import tensorflow.python directly in application code.
  • Verify TensorFlow is installed in the exact interpreter you are running.
  • Use a clean virtual environment to eliminate version and path confusion.
  • Check for local files that shadow the real TensorFlow package.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.