Last modified: Aug 11, 2026
Fix ModuleNotFoundError: No module named 'tensorflow'
Encountering ModuleNotFoundError: No module named 'tensorflow' is a common issue for Python developers. This error stops your script immediately when TensorFlow isn't installed or recognized in your current environment.
The good news is that this error is easy to fix. You just need to understand your Python environment and install the package correctly. This guide walks you through every solution, from basic installation to advanced troubleshooting.
Why Does This Error Occur?
The error appears when Python can't find the TensorFlow library in its search path. This happens for several reasons:
- TensorFlow isn't installed at all.
- You're using a different Python interpreter than the one where TensorFlow is installed.
- Your virtual environment is not activated.
- Your pip version is outdated.
- You're using a Python version that TensorFlow doesn't support.
Let's solve each of these systematically.
Quick Fix: Install TensorFlow
The most straightforward solution is to install TensorFlow using pip. Open your terminal or command prompt and run this command:
pip install tensorflow
If you're using Python 3, you might need to use pip3 instead:
pip3 install tensorflow
This installs the latest stable version of TensorFlow. After installation, try importing it again:
import tensorflow as tf
print(tf.__version__)
Output:
2.16.1
If you see a version number, the error is resolved. If not, continue to the next steps.
Check Your Python Environment
Sometimes you have multiple Python installations. Your terminal might use one, while your IDE uses another. This mismatch causes the import error.
First, check which Python you're using:
which python
which python3
Then check where pip installs packages:
python -m pip --version
python3 -m pip --version
If these paths differ, you're installing to the wrong location. Always use python -m pip install to ensure you're installing to the same interpreter you're using:
python -m pip install tensorflow
This command guarantees that pip installs packages for the exact Python interpreter that runs your code.
Use a Virtual Environment
Virtual environments isolate your project dependencies. This prevents conflicts between different projects and ensures TensorFlow is available where you need it.
Create a virtual environment:
python -m venv tf_env
Activate it:
- On Windows:
tf_env\Scripts\activate - On macOS/Linux:
source tf_env/bin/activate
Now install TensorFlow inside this environment:
pip install tensorflow
Your terminal prompt should show (tf_env) at the beginning, indicating the environment is active. Now run your Python script, and the import should work.
For more on managing Python environments, check out our guide on Python virtual environments.
Upgrade pip and Setuptools
An outdated pip can cause installation failures. TensorFlow might not install correctly if pip can't resolve dependencies.
Upgrade pip first:
pip install --upgrade pip setuptools wheel
Then try installing TensorFlow again:
pip install tensorflow
This often resolves hidden dependency issues that prevent TensorFlow from being imported.
Verify Python Version Compatibility
TensorFlow requires a specific Python version. If you're using an unsupported version, the installation might fail or the import might not work.
Check your Python version:
python --version
TensorFlow 2.x supports Python 3.8 through 3.12. If you're using Python 3.13 or newer, you might need to install an older TensorFlow version:
pip install tensorflow==2.15.0
Alternatively, install a compatible Python version and create a new environment.
Use Conda Instead of pip
If pip doesn't work, conda is a reliable alternative. Conda handles binary dependencies better, especially on Windows.
Install TensorFlow with conda:
conda install tensorflow
Or create a new conda environment:
conda create -n tf_env python=3.10
conda activate tf_env
conda install tensorflow
Conda ensures that all dependencies are compatible, reducing the chance of import errors.
Check for Conflicting Package Names
Sometimes a package named tensorflow exists in your environment but is corrupted or incomplete. This can happen after a failed installation.
Uninstall and reinstall TensorFlow:
pip uninstall tensorflow
pip install tensorflow --no-cache-dir
The --no-cache-dir flag forces pip to download fresh files, bypassing any corrupted cached versions.
Check Your IDE's Python Interpreter
If you're using Visual Studio Code, PyCharm, or Jupyter, they might use a different Python interpreter than your terminal.
In VS Code, open the Command Palette (Ctrl+Shift+P) and select Python: Select Interpreter. Choose the one where you installed TensorFlow.
In PyCharm, go to File → Settings → Project → Python Interpreter and verify the correct environment is selected.
For Jupyter, use %pip install tensorflow directly in a cell to install to the kernel's environment:
%pip install tensorflow
This ensures the installation goes to the same environment where your notebook runs.
Check for Hardware-Specific Versions
If you have a GPU and want GPU support, you need the right TensorFlow version:
pip install tensorflow-gpu
Note that for TensorFlow 2.x, tensorflow includes GPU support by default if CUDA and cuDNN are installed. You don't need a separate package.
Verify GPU availability:
import tensorflow as tf
print(tf.config.list_physical_devices('GPU'))
This outputs a list of GPU devices if detected, or an empty list if not.
Advanced: Install from Source
If you need the latest development version or have special requirements, build TensorFlow from source. This is complex but gives you full control.
First, clone the repository:
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
Then configure and build:
./configure
bazel build //tensorflow/tools/pip_package:build_pip_package
This process takes time and requires Bazel, but it gives you a custom build optimized for your system.
Common Error Messages and Fixes
Here are specific error variants and their solutions:
Error: "No module named 'tensorflow_core'"
This usually means you have an old TensorFlow version. Upgrade to the latest:
pip install --upgrade tensorflow
Error: "ImportError: DLL load failed" (Windows)
This indicates missing Visual C++ redistributables. Install the Microsoft Visual C++ Redistributable from Microsoft's official site.
Error: "Illegal instruction (core dumped)"
Your CPU might not support certain instructions. Install a CPU-only version:
pip install tensorflow-cpu
Test Your Installation
After following these steps, create a simple test script to confirm everything works:
import tensorflow as tf
# Create a simple constant
hello = tf.constant('Hello, TensorFlow!')
# Start a session and print
print(hello.numpy().decode())
Output:
Hello, TensorFlow!
If you see this output, your TensorFlow installation is working correctly.
Prevent Future Issues
To avoid this error in the future, follow these best practices:
- Always use a virtual environment for your projects.
- Document your dependencies in a
requirements.txtfile. - Use
python -m pipinstead of barepip. - Regularly update pip and setuptools.
- Check your Python version before installing.
For more Python troubleshooting, see our guide on common import errors in Python.
Conclusion
The ModuleNotFoundError: No module named 'tensorflow' error has a clear set of solutions. Start with the basic pip install, then verify your environment, and finally check for version compatibility.
Most users resolve this within minutes by installing TensorFlow in the correct environment. If you're still stuck, revisit the steps and confirm you're using the right Python interpreter. Remember to always work in a virtual environment to keep your projects clean and avoid dependency conflicts.
With TensorFlow properly installed, you can now build and train machine learning models without interruption.