tensorflow
ImportError
tensorflow-gpu
python
module-error

ImportError No module named 'tensorflow.python' with tensorflow-gpu

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

This error usually means the TensorFlow installation is broken, mixed across environments, or based on outdated packaging assumptions. The most important current fact is that tensorflow-gpu is no longer the package most users should install. Modern TensorFlow installs use the tensorflow package, with GPU support handled through the supported platform-specific setup.

Why the Error Happens

tensorflow.python is an internal package inside the TensorFlow distribution. When Python says it cannot find it, one of these situations is usually true:

  • the TensorFlow install is incomplete or corrupted
  • 'tensorflow and tensorflow-gpu were mixed in the same environment'
  • the wrong Python interpreter is running
  • a local file such as tensorflow.py is shadowing the real package
  • code is importing TensorFlow internals directly instead of using the public API

A clean environment solves most cases faster than trying to patch an already broken one.

Stop Installing tensorflow-gpu by Default

Older tutorials tell users to install:

bash
pip install tensorflow-gpu

That advice is outdated. The modern package is:

bash
python -m pip install tensorflow

On supported Linux setups, current TensorFlow documentation also provides an extra for NVIDIA CUDA dependencies:

bash
python -m pip install "tensorflow[and-cuda]"

If you already have tensorflow-gpu in the environment, remove it along with any conflicting TensorFlow packages before reinstalling.

Create a Clean Environment

The safest repair path is a fresh virtual environment:

bash
1python -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip
4python -m pip uninstall -y tensorflow tensorflow-gpu
5python -m pip install tensorflow

Then verify the import:

bash
1python - <<'PY'
2import tensorflow as tf
3print(tf.__version__)
4print(tf.config.list_physical_devices("GPU"))
5PY

If the import works but the GPU list is empty, the package is installed and the remaining issue is CUDA or platform support, not the tensorflow.python import path itself.

Use the Public API Only

Do this:

python
import tensorflow as tf

model = tf.keras.Sequential()

Do not do this:

python
from tensorflow.python import something

tensorflow.python is an internal implementation detail. Even if that import works in one environment, it is not the stable public API TensorFlow expects user code to depend on.

Check for Local Shadowing

Python may be importing the wrong thing if your project contains files such as:

  • 'tensorflow.py'
  • a folder named tensorflow
  • stale __pycache__ entries from earlier experiments

Check what Python is loading:

bash
1python - <<'PY'
2import tensorflow
3print(tensorflow.__file__)
4PY

If that path points into your project instead of the virtual environment’s site-packages directory, rename the local file or folder.

Platform Reality Matters

TensorFlow GPU support depends on platform. Current official support is strongest on Linux. Windows users often need WSL2 for current GPU workflows, and macOS does not have the same official NVIDIA CUDA path. If you are following an old “tensorflow-gpu on native Windows” tutorial, that mismatch alone can lead you into unsupported combinations.

Common Pitfalls

The most common mistake is mixing tensorflow, tensorflow-gpu, CUDA toolkits, and old tutorials in the same environment.

Another frequent problem is using one pip and a different python, which installs TensorFlow into one interpreter and runs code from another.

Direct imports from tensorflow.python are also a recurring issue. Even when they work temporarily, they are not the supported public interface.

Finally, do not keep repairing a heavily polluted environment forever. A fresh virtual environment is often the fastest fix.

Summary

  • 'ImportError: No module named 'tensorflow.python' usually points to a bad or mixed TensorFlow installation.'
  • Modern TensorFlow setups should usually install tensorflow, not tensorflow-gpu.
  • Use a clean virtual environment and verify the import with import tensorflow as tf.
  • Check for local files that shadow the real TensorFlow package.
  • Import only the public TensorFlow API, not internal tensorflow.python modules.

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.