Python
TensorFlow
iPython
ErrorFixing
MachineLearning

No module named tensor flow -- iPython notebook

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

If a Jupyter or IPython notebook says No module named tensorflow, the notebook is almost always using a different Python environment from the one where TensorFlow was installed. The fix is not usually inside the import statement itself; it is making the notebook kernel, the interpreter, and the package installation all point to the same environment.

Check Which Python the Notebook Is Using

Start inside the notebook, not in the terminal. You need to see the interpreter path of the running kernel.

python
import sys
print(sys.executable)
print(sys.version)

Then compare that with the environment you expect from the shell.

bash
python --version
python -m pip show tensorflow
which python

On Windows, use where python instead of which python.

If the notebook path and shell path differ, you have already found the likely cause.

Install TensorFlow Into That Exact Interpreter

The safest installation pattern is to invoke pip through the interpreter you actually want.

bash
python -m pip install --upgrade pip
python -m pip install tensorflow

This is better than relying on a bare pip command, which may point to a different Python installation.

You can even verify the package from inside the notebook kernel itself:

python
import sys
!{sys.executable} -m pip show tensorflow

That command asks the currently running kernel's Python to inspect its own installed packages, which is usually more trustworthy than checking from a separate shell window.

Register the Environment as a Notebook Kernel

Even after installing TensorFlow, the notebook may still be attached to the wrong kernel. The clean fix is to register the environment explicitly with ipykernel.

bash
python -m pip install ipykernel
python -m ipykernel install --user --name tf-env --display-name "Python (tf-env)"

After that, restart Jupyter and choose Python (tf-env) from the kernel list.

This is one of the most common missing steps. Developers install TensorFlow successfully, but the notebook keeps using an older system Python or a different virtual environment.

Use a Clean Virtual Environment When in Doubt

If the machine already has several Python installations, starting fresh is often faster than untangling them.

bash
1python -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip
4python -m pip install tensorflow ipykernel
5python -m ipykernel install --user --name tf-notebook --display-name "Python (tf-notebook)"

On Windows PowerShell, the activation step usually looks like this:

powershell
1py -m venv .venv
2.\.venv\Scripts\Activate.ps1
3py -m pip install tensorflow ipykernel
4py -m ipykernel install --user --name tf-notebook --display-name "Python (tf-notebook)"

Once the environment and kernel are aligned, the import should work predictably.

Confirm the Import in the Notebook

After switching kernels, verify the import and the package version directly in a cell.

python
import tensorflow as tf
print(tf.__version__)

If that still fails, re-check sys.executable. Many notebook import problems come from assuming the selected kernel changed when it actually did not.

Common Pitfalls

The first pitfall is running pip install tensorflow in one terminal while the notebook kernel points to a different interpreter. The installation succeeds, but not for the environment the notebook is using.

Another issue is mixing conda, system Python, pyenv, and virtual environments without verifying which one Jupyter is attached to.

Developers also often forget to restart the notebook kernel after installing packages. The environment may be fixed, but the running kernel still has stale state.

Finally, the module name is tensorflow, not tensor flow and not a two-word import. If the environment is correct, the import statement should simply be import tensorflow as tf.

Summary

  • The error usually means the notebook kernel and the TensorFlow installation are in different Python environments.
  • Check sys.executable inside the notebook before doing anything else.
  • Install TensorFlow with python -m pip for the exact interpreter you want.
  • Register that environment as a Jupyter kernel with ipykernel.
  • Restart the kernel and verify the import directly in the notebook.

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.