Recently, I was working on a machine learning project using TensorFlow Hub for transfer learning when I encountered this error: ModuleNotFoundError: No module named ‘tensorflow_hub’. This error occurs when Python cannot find the TensorFlow Hub module in your environment.
The good news is that this is a common issue with simple solutions.
In this article, I will discuss several methods to resolve this error, drawing on my ten years of experience in Python development. Let’s get started and ensure your code runs correctly!
TensorFlow Hub
TensorFlow Hub is a library for reusing pre-trained machine learning models. It allows you to import trained model components that you can use in your models without having to train them from scratch.
This is particularly useful for transfer learning, where you leverage a model trained on one task to perform well on another related task.
Method 1: Install TensorFlow Hub Using pip
The most common reason for the ModuleNotFoundError: no module named 'tensorflow_hub' error is that the module simply isn’t installed in your Python environment. Here’s how to fix it:
pip install tensorflow-hubAfter installation, try importing the module again:
import tensorflow_hub as hub
print(hub.__version__)I executed the above example code and added the screenshot below.

If the installation was successful, you should see the version of TensorFlow Hub printed without any errors.
Read ModuleNotFoundError: No module named ‘tensorflow.contrib’
Method 2: Install Using conda (For Anaconda Users)
If you’re using Anaconda for managing your Python environments, you can install TensorFlow Hub using conda:
conda install -c conda-forge tensorflow-hubAfter installation, verify it worked:
import tensorflow_hub as hub
print("TensorFlow Hub version:", hub.__version__)I executed the above example code and added the screenshot below.

Check out Fix ModuleNotFoundError: No module named ‘tensorflow.compat’
Method 3: Check Your Python Environment
Sometimes the issue isn’t with the installation but with using the wrong Python environment. This is especially common when you have multiple Python environments on your system.
Here’s how to check which environment you’re using:
import sys
print(sys.executable)This will show you the path to the Python interpreter being used. Make sure you install TensorFlow Hub in the correct environment.
Method 4: Verify TensorFlow Installation
TensorFlow Hub requires TensorFlow to be installed. If you haven’t installed TensorFlow or if your TensorFlow version is incompatible with the TensorFlow Hub version, you might encounter errors.
First, check if TensorFlow is installed:
import tensorflow as tf
print(tf.__version__)I executed the above example code and added the screenshot below.

If TensorFlow isn’t installed or if you need to update it:
pip install tensorflow --upgradeThen install TensorFlow Hub:
pip install tensorflow-hub --upgradeRead Module ‘keras.backend’ has no attribute ‘set_session’
Method 5: Virtual Environment Solution
I’ve found that creating a clean virtual environment often solves dependency issues. Here’s how:
# Create a new virtual environment
python -m venv tf_hub_env
# Activate the environment
# On Windows
tf_hub_env\Scripts\activate
# On macOS/Linux
source tf_hub_env/bin/activate
# Install TensorFlow and TensorFlow Hub
pip install tensorflow
pip install tensorflow-hubThis creates an isolated environment where you can install packages without affecting your global Python installation.
Method 6: Fix Import Syntax
Sometimes the error occurs because of incorrect import syntax. Make sure you’re using the correct import statement:
# Correct import
import tensorflow_hub as hub
# Not this
import tensorflow.hub # IncorrectOnce you’ve installed TensorFlow Hub correctly, you should import it directly as shown above.
Check out AttributeError: Module ‘keras.backend’ has no attribute ‘get_session’
Method 7: Check for Version Compatibility
TensorFlow Hub needs to be compatible with your TensorFlow version. Here’s a general compatibility guide:
- TensorFlow 2.x → TensorFlow Hub 0.5.0+
- TensorFlow 1.15 → TensorFlow Hub 0.7.0 or 0.8.0
If you’re using an older version of TensorFlow, you might need to install a specific version of TensorFlow Hub:
pip install tensorflow-hub==0.8.0 # For TensorFlow 1.15A Real-World Example
Let’s say you’re building a sentiment analysis model for customer reviews of a popular U.S. smartphone. You want to use a pre-trained model from TensorFlow Hub to save time:
import tensorflow as tf
import tensorflow_hub as hub
# Load a pre-trained text embedding model
embedding_url = "https://tfhub.dev/google/nnlm-en-dim50/2"
embedding_layer = hub.KerasLayer(embedding_url, input_shape=[], dtype=tf.string)
# Create a simple model
model = tf.keras.Sequential([
embedding_layer,
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
# Sample data (smartphone reviews)
reviews = [
"This phone has amazing battery life and the camera is outstanding",
"Worst purchase ever, it stopped working after one week",
"Good value for money but the screen scratches easily"
]
sentiments = [1, 0, 0.5] # 1 = positive, 0 = negative, 0.5 = neutral
# Compile the model
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['accuracy'])
# Convert data to tensors
reviews_tensor = tf.constant(reviews)
sentiments_tensor = tf.constant(sentiments)
# Train the model
model.fit(reviews_tensor, sentiments_tensor, epochs=10)This example shows how TensorFlow Hub simplifies leveraging pre-trained models for specific tasks.
Troubleshoot Other Common Issues
If you’re still experiencing issues after trying the above methods, here are some additional troubleshooting steps:
Read Fix Module ‘TensorFlow’ has no attribute ‘session’
Proxy Issues
If you’re behind a corporate firewall:
pip install tensorflow-hub --proxy=http://your-proxy:portGPU Support
If you’re using TensorFlow with GPU support, ensure you have the appropriate CUDA and cuDNN versions installed for your TensorFlow version.
Jupyter Notebook Specific Fix
When working in Jupyter Notebook, you might need to restart the kernel after installing a new package:
- Install the package
!pip install tensorflow-hub- Restart the kernel using the menu: Kernel > Restart
- Then try importing again:
import tensorflow_hub as hubThis step is often overlooked but is necessary.
Fixing the ModuleNotFoundError: no module named 'tensorflow_hub' is usually simple once you understand the potential causes. In most cases, a simple pip installation solves the problem.
Remember to check your Python environment, ensure compatibility between TensorFlow and TensorFlow Hub versions, and consider using a clean virtual environment for your projects.
Other Python articles you may also like:
- Module ‘TensorFlow’ has no attribute ‘get_default_graph’
- AttributeError: Module ‘tensorflow’ has no attribute ‘logging’
- AttributeError module ‘tensorflow’ has no attribute ‘summary’

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.