As a developer, while working on a legacy TensorFlow project, and encountered the error “AttributeError: module ‘tensorflow’ has no attribute ‘get_variable’” when trying to run my code. This error typically occurs when you’re using TensorFlow 2.x but your code was written for TensorFlow 1.x.
In this tutorial, I will share multiple solutions to fix this error based on my experience working with both TensorFlow versions. You’ll learn how to adapt your code to work with modern TensorFlow while preserving functionality.
Let’s get started..!
Why Does This Error Occur?
Python’s get_variable() function was a fundamental part of TensorFlow 1.x, but it was removed in TensorFlow 2.0 as part of API cleanup and simplification.
If you’re running code that was written for TensorFlow 1.x on a newer installation, you’ll encounter this error when the code tries to call tf.get_variable().
Solution 1: Use TensorFlow Compatibility Mode
The easiest way to fix this error without changing your code is to use TensorFlow’s compatibility module:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
variable = tf.get_variable(name="my_variable", shape=[1], initializer=tf.zeros_initializer())
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
value = sess.run(variable)You can refer to the screenshot below to see the output:

This solution is particularly useful when you’re working with a large codebase that would be difficult to update all at once. It tells TensorFlow to use the older API style.
Solution 2: Update to TensorFlow 2.x API
A better long-term solution is to update your code to use the TensorFlow 2.x API, which replaces get_variable() with the Variable() constructor:
import tensorflow as tf
# Modern TF2.x approach
variable = tf.Variable(initial_value=tf.zeros([1]), name="my_variable")
print(variable)You can refer to the screenshot below to see the output:

This is the recommended approach for new projects or when you’re ready to modernize your codebase.
Check out Module ‘tensorflow’ has no attribute ‘truncated_normal’
Solution 3: Access get_variable from compat.v1 Without Disabling v2
If you only need to use Python’s get_variable() method in a few places but want to keep using the TensorFlow 2.x API elsewhere, you can selectively access the function from the tf.compat.v1 module:
import tensorflow as tf
# Use get_variable from compat.v1 without disabling v2 behavior
variable = tf.compat.v1.get_variable(name="my_variable", shape=[1], initializer=tf.zeros_initializer())
# Rest of your code can use TF2.x API
modern_var = tf.Variable(initial_value=[1.0], name="modern_variable")
print(variable)
print(modern_var)You can refer to the screenshot below to see the output:

This approach gives you the flexibility to migrate your code gradually.
Read AttributeError: Module ‘tensorflow’ has no attribute ‘trainable_variables’
Solution 4: Install the Correct TensorFlow Version
If you need to run legacy code without modifications, you can install TensorFlow 1.x:
pip uninstall tensorflow
pip install tensorflow==1.15.0After installing TensorFlow 1.x, the original code using tf.get_variable() should work without modifications:
import tensorflow as tf
# Original TF1.x code
variable = tf.get_variable(name="my_variable", shape=[1], initializer=tf.zeros_initializer())
print(variable)However, I don’t recommend this approach for new projects as TensorFlow 1.x is no longer actively maintained.
Check out AttributeError: Module ‘tensorflow’ has no attribute ‘truncated_normal_initializer’
Real-world Example: Migrate a Neural Network Model
Let me show you a practical example of migrating a neural network from TensorFlow 1.x to 2.x. Here’s how you might have defined a simple layer in TensorFlow 1.x:
# TensorFlow 1.x approach
def create_layer(inputs, input_size, output_size, name):
with tf.variable_scope(name):
weights = tf.get_variable("weights", shape=[input_size, output_size],
initializer=tf.truncated_normal_initializer(stddev=0.1))
biases = tf.get_variable("biases", shape=[output_size],
initializer=tf.constant_initializer(0.0))
return tf.matmul(inputs, weights) + biasesHere’s how you would rewrite this using the TensorFlow 2.x API:
# TensorFlow 2.x approach
def create_layer(inputs, input_size, output_size, name):
# Use tf.keras layers instead
layer = tf.keras.layers.Dense(
output_size,
kernel_initializer=tf.keras.initializers.RandomNormal(stddev=0.1),
bias_initializer='zeros',
name=name
)
return layer(inputs)Handle Variable Scopes in TensorFlow 2.x
Another common issue related to get_variable() is the use of variable scopes. In TensorFlow 1.x, you might have code like this:
# TensorFlow 1.x with variable scopes
with tf.variable_scope("model", reuse=tf.AUTO_REUSE):
x = tf.get_variable("x", shape=[])In TensorFlow 2.x, variable scopes are replaced with name scopes and variable names:
# TensorFlow 2.x approach
with tf.name_scope("model"):
x = tf.Variable(initial_value=0.0, name="x")If you encounter the error “Module ‘tensorflow’ has no attribute ‘variable_scope'”, you’ll need to apply similar solutions as discussed for get_variable().
The solutions for these errors follow the same pattern: either use the compatibility module or update to the TensorFlow 2.x API.
I hope this guide has helped resolve your “module ‘tensorflow’ has no attribute ‘get_variable'” error. Remember that while compatibility mode is convenient for quick fixes, migrating to the modern TensorFlow 2.x API will give you access to better performance, improved features, and continued support.
Other tensorflow-tutorial, you may read:
- Module ‘tensorflow’ has no attribute ‘optimizers’
- Module ‘tensorflow’ has no attribute ‘log’
- AttributeError: Module ‘tensorflow’ has no attribute ‘global_variables_initializer’

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.