ModuleNotFoundError No module named 'tf'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
ModuleNotFoundError: No module named 'tf' usually has a simple cause: Python is trying to import a package literally named tf, but TensorFlow is installed under the package name tensorflow. The conventional alias tf is created only after you import tensorflow as tf.
Use the Correct Import Statement
This is wrong:
This is correct:
The as tf part creates the short alias in your current module. It does not mean there is a standalone package called tf on disk.
Install TensorFlow in the Active Environment
If import tensorflow as tf also fails, TensorFlow is missing from the interpreter you are using. Verify installation with the same Python executable that runs your script.
If the package is missing, install it:
Using python -m pip is safer than a bare pip command because it reduces the chance of installing into a different Python environment than the one your program uses.
Watch for Environment and Naming Conflicts
Even with the right import statement, import problems can persist if your project is using the wrong interpreter or if a local file shadows a real package.
For example, this project layout can create confusion:
If you create a local file named tensorflow.py, Python may import that file instead of the real package. Rename the file and delete any stale __pycache__ directory if necessary.
Virtual environments can cause similar confusion. Your shell, your editor, and your notebook kernel all need to point at the same Python installation if you want imports to behave consistently.
Test the Alias in a Minimal Script
When the error is unclear, reduce the problem to the smallest possible script:
If this script runs, TensorFlow is installed correctly and the bug is somewhere in the original project, not in Python's import system.
For team projects, pin the dependency in requirements.txt or your environment manager so new machines do not fail with the same import issue. Reproducible environments eliminate a large share of "works on my machine" TensorFlow problems.
Common Pitfalls
The biggest mistake is trying to solve the problem with pip install tf. That usually installs the wrong package or no useful package at all because the TensorFlow project is published as tensorflow.
Another common issue is installing TensorFlow in one environment and running the code in another. This happens frequently in notebooks, IDEs, and shells that each use different interpreters.
Local module shadowing is also common. Files named tensorflow.py, folders named tensorflow, or custom site-packages hacks can make a correct import look broken.
Finally, after installing packages in a notebook, restart the kernel. The current session may not see the newly installed package until it reloads the interpreter.
Summary
- '
tfis an alias created byimport tensorflow as tf, not a package you import directly.' - Use
import tensorflow as tf, notimport tf. - Install TensorFlow with
python -m pip install tensorflowin the active environment. - Check for interpreter mismatches and local files that shadow the real package.
- Reduce the problem to a minimal script when a larger project hides the real cause.

