Tensorflow AttributeError: ‘module’ object has no attribute ‘placeholder’ is an error which occurs when your tensorflow installation has compatibility problems or when you have a faulty Tensorflow installation.
I will explain why this error is taking place and how to fix it, while also trying to add other solutions that could help us solve the problem.
Exploring Tensorflow AttributeError: ‘module’ object has no attribute ‘placeholder’
This is an error which occurs when your tensorflow installation has compatibility problems or when you have a faulty Tensorflow installation.
Please double check so you can avoid mixing between errors. The error message should look like the error message bellow.
# AttributeError: 'module' object has no attribute 'placeholder' #
Bellow I have a couple of methods that fixed the issue for me, I hope they do the same for you.
Solution 1 : remove tensorflow-gpu and all versions of Tensorflow then start with a fresh install
The first solution is for those of you who have installed tensorflow-gpu. If you do not have it, please skip to the next solution.
This is one of the most popular reasons why devs encounter this particular error, if you do not have a good GPU you do not want to install the GPU version of Tensorflow, since it is going to cause all sorts of problems and errors.
If you have a machine with a normal CPU. Just use the normal version of Tensorflow.
You can use conda to remove Tensorflow GPU, just like this.
# conda remove tensorflow-gpu #
You can add the following command to make sure you do not have any version of Tensorflow on your device.
# conda remove tensorflow tensorflow-base #
After removing all versions of Tensorflow, try to install Tensorflow again using the command bellow.
# conda install tensorflow #
I hope this was enough to solve the issue. If that was not the case please try the second method.
Solution 2 : disable Tensorflow v2 behavior
The second method to fix the issue is to disable Tensorflow v2 behavior.
To do that, please use
# import tensorflow.compat.v1 as tf #
Instead of using.
# import tensorflow as tf #
Now, you should add this line of code after importing Tensorflow.
# tf.disable_v2_behavior() #
I hope that disabling Tensorflow v2 behavior has fixed your issue.
I hope the two methods above fixed your problem, good luck with your next python projects and consider donating to our Kofi account.
Summing-up :
Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/