Recently, I was working on a deep learning project using TensorFlow when I encountered this frustrating error message: AttributeError: module ‘tensorflow’ has no attribute ‘py_function’. This error typically occurs when you’re trying to use TensorFlow 1.x code with TensorFlow 2.x, or when your TensorFlow installation has compatibility issues.
The problem is that the py_function API has changed between TensorFlow versions, confusing for many developers.
In this article, I’ll walk you through several practical methods to solve this error based on my experience. I’ll show you what causes this issue and provide step-by-step solutions to get your code running smoothly again.
Understand the Error
Before jumping into solutions, it’s important to understand why this error occurs.
The py_function attribute was relocated in TensorFlow 2.x. In TensorFlow 1.x, you could access it directly through the main module, but in TensorFlow 2.x, it’s now part of the tf.compat.v1 namespace or available through other methods.
Let’s look at how to fix this issue using different approaches.
Read AttributeError: Module ‘keras.optimizers’ has no attribute ‘rmsprop’
Method 1: Use the Correct TensorFlow 2.x Path
In TensorFlow 2.x, py_function is available under the tf.numpy_function or tf.experimental.numpy.function namespace.
Here’s how to properly use it:
import tensorflow as tf
import numpy as np
# Define a regular Python function
def my_python_function(x, y):
return np.array(x + y, dtype=np.float32)
# Sample inputs as tensors
input1 = tf.constant(3.0)
input2 = tf.constant(4.0)
# Use tf.numpy_function (TF 2.x way)
result = tf.numpy_function(func=my_python_function, inp=[input1, input2], Tout=tf.float32)
# Print the result (eager execution)
print("Result from tf.numpy_function:", result.numpy())Output:
Result from tf.numpy_function: 7.0I executed the above example code and added the screenshot below.

This simple change in the path often resolves the issue immediately.
Check out AttributeError: Module ‘tensorflow’ has no attribute ‘dimension’
Method 2: Use Compatibility Mode
TensorFlow 2.x includes a compatibility module that allows you to run TensorFlow 1.x code with minimal changes.
Here’s how to use it:
import tensorflow.compat.v1 as tf
import numpy as np
tf.disable_v2_behavior()
# Define a Python function
def my_python_function(x, y):
return np.array(x + y, dtype=np.float32)
# Sample inputs as constants
input1 = tf.constant(5.0)
input2 = tf.constant(7.0)
# Use tf.py_function in TF 1.x compatibility mode
result = tf.py_function(func=my_python_function, inp=[input1, input2], Tout=tf.float32)
# Launch a session to evaluate the result
with tf.Session() as sess:
output = sess.run(result)
print("Result from tf.py_function in TF 1.x mode:", output)Output:
Result from tf.py_function in TF 1.x mode: 12.0I executed the above example code and added the screenshot below.

This approach is particularly useful when you’re working with legacy code that would require extensive rewriting to be fully compatible with TensorFlow 2.x.
Read AttributeError: module ‘tensorflow.keras.layers’ has no attribute ‘multiheadattention’
Method 3: Downgrade to TensorFlow 1.x
If you’re working with a codebase that heavily relies on TensorFlow 1.x features, and you don’t have time to migrate it, downgrading your TensorFlow installation might be a practical solution.
Here’s how to install a specific version of TensorFlow:
pip uninstall tensorflow tensorflow-gpu
pip install tensorflow==1.15.0After downgrading, restart your Python environment and verify the installed version:
import tensorflow as tf
print(tf.__version__) I executed the above example code and added the screenshot below.

This approach should be considered a temporary solution as TensorFlow 1.x is no longer receiving new features.
Read Solve the ModuleNotFoundError: no module named ‘tensorflow_hub’
Method 4: Update Your Code to TensorFlow 2.x Standards
The most forward-compatible solution is to update your code to follow TensorFlow 2.x patterns. Instead of using py_function consider using TensorFlow’s autograph capabilities, which convert Python code to TensorFlow graph code.
Here’s an example of how to refactor your code:
import tensorflow as tf
# Old way with py_function
# def process_data(x):
# # Some Python processing
# return result
# output = tf.py_function(process_data, [input], tf.float32)
# New way with tf.function
@tf.function
def process_data(x):
# Your processing logic here
return x * 2 # Example operation
# Use the function directly
input_tensor = tf.constant([1.0, 2.0, 3.0])
output = process_data(input_tensor)The @tf.function decorator compiles a Python function into a TensorFlow graph, which often provides better performance and compatibility.
Check out ModuleNotFoundError: No module named ‘tensorflow.contrib’
Method 5: Create a Custom Implementation
If you need specific functionality from py_function that isn’t directly available, you can create a wrapper function:
import tensorflow as tf
import numpy as np
def custom_py_function(func, inp, Tout):
"""
A custom implementation to replace tf.py_function
"""
def wrapped_func(*args):
# Convert TensorFlow tensors to NumPy arrays for the Python function
numpy_args = [arg.numpy() for arg in args]
# Call the Python function
result = func(*numpy_args)
# Convert the result back to a TensorFlow tensor
return tf.convert_to_tensor(result, dtype=Tout)
# Use tf.numpy_function as the backend
return tf.numpy_function(wrapped_func, inp, Tout)
# Usage example
def multiply_and_add(x, y):
return np.multiply(x, y) + 5
a = tf.constant([1, 2, 3])
b = tf.constant([4, 5, 6])
result = custom_py_function(multiply_and_add, [a, b], tf.int32)This approach gives you more control over the conversion process between TensorFlow tensors and NumPy arrays.
Real-World Example: Image Data Augmentation
Let’s look at a practical example from a project I worked on for a U.S. healthcare company. We needed to preprocess medical images with custom Python functions before feeding them into a neural network:
import tensorflow as tf
import numpy as np
import cv2
# In TensorFlow 1.x, this would cause an error in TensorFlow 2.x
# def augment_image(image):
# # Custom OpenCV operations
# img = cv2.GaussianBlur(image, (5, 5), 0)
# return img
# augmented = tf.py_function(augment_image, [original_image], tf.float32)
# TensorFlow 2.x solution
def augment_image(image):
# Custom OpenCV operations
img = cv2.GaussianBlur(image.numpy(), (5, 5), 0)
return img
# Create a dataset with medical images
dataset = tf.data.Dataset.from_tensor_slices([...]) # Your medical images
# Apply the augmentation using the correct TensorFlow 2.x method
augmented_dataset = dataset.map(
lambda x: tf.numpy_function(augment_image, [x], tf.float32)
)
# Build and train the model
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy')
model.fit(augmented_dataset, epochs=10)This example demonstrates how to properly implement custom image processing in TensorFlow 2.x data pipelines.
Check out Fix ModuleNotFoundError: No module named ‘tensorflow.compat’
Troubleshoot Common Issues
If you’re still experiencing issues after trying these methods, here are some additional troubleshooting steps:
- Check your TensorFlow version: Ensure you know exactly which version you’re running:
import tensorflow as tf
print(tf.__version__)- Verify environment isolation: Make sure you don’t have multiple TensorFlow installations conflicting with each other. Consider using a virtual environment or a conda environment.
- Update TensorFlow: Sometimes, updating to the latest version resolves compatibility issues:
pip install --upgrade tensorflow- Check for typos: Ensure you’re using the correct spelling and capitalization in your code.
I hope this article has helped you solve the AttributeError: module 'tensorflow' has no attribute 'py_function' error. Working with deep learning frameworks often involves dealing with these kinds of version compatibility issues, but with the right approach, they can be overcome.
By understanding the root cause and applying one of these solutions, you should be able to get your TensorFlow code running smoothly again. Remember that migrating to the TensorFlow 2.x API standards is the most future-proof solution, though the compatibility approaches can help maintain legacy code.
Other Python articles you may also like:
- Module ‘keras.backend’ has no attribute ‘set_session’
- AttributeError: Module ‘keras.backend’ has no attribute ‘get_session’
- Fix Module ‘TensorFlow’ has no attribute ‘session’

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.