How to resolve ModuleNotFoundError: No module named 'tensorflow'

Often you may encounter, ModuleNotFoundError: No module named ‘tensorflow’, error when you are trying to run a Python script that uses TensorFlow for a Machine learning and deep learning projects. This means Python can’t find the TensorFlow library in your current environment. Which usually happens when:
1
2
3
4
TensorFlow is not installed in the environment being used (i.e., virtual environment)
You're using the wrong Python environment (and so TensorFlow may be installed elsewhere)
There's a typo in the module name
Incorrect system PATH configuration, preventing Python from detecting installed packages
Follow the steps below to fix the Issue:
1. Install TensorFlow
The simplest fix is to install TensorFlow using pip by opening your terminal or command prompt and run:
1
pip install tensorflow
If you’re in a Jupyternotebook, run:
1
!pip install tensorflow
2. Check Your Environment
If you’re using a virtual environment (like venv
or conda
), make sure it’s activated before installing or running or your code”
1
2
3
4
5
6
7
# For venv
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
# For conda
conda activate myenv
3. Configure Your System Path
If Python still can’t locate TensorFlow, verify that the Python executable and package installation directories are included in your system’s PATH
4. Reinstall (If needed)
If TensorFlow is already installed but you still get the error, try reinstalling by:
1
2
3
pip uninstall tensorflow
pip install tensorflow
Note that, this ModuleNotFoundError issue is a common setup challenge. Which is easily resolved when you follow the above steps.
Thanks for reading!