How to Fix “AttributeError: module ‘tensorflow’ has no attribute ‘placeholder’.” in Visual Studio Code

If you are new to Python and on your way to trying out in making a Machine Learning script and using TensorFlow. As you go your way in coding, and decided to test and run the program. An error appeared stating “AttributeError: Module 'Tensorflow' Has No Attribute 'Placeholder'“. Then this article will provide you the fix in your issue.

This error states that you have used an import that causes your code to not run. Which in this case is the TensorFlow import, where the version that the attribute ‘placeholder’ that you used isn’t working in the TensorFlow 2.0.

Thus, all you have to do is go to your code and replace the line

import tensorflow as tf

change the line above into this line

import tensorflow.compat.v1 as tf

The line above bypasses the TenserFlow 2.0 version error and allows the use of the previous TenserFlow versions attribute to be used.

Afterwards, re-run your program and that should do it.

That’s it. Hoping this assists you in fixing your error. “AttributeError: Module 'Tensorflow' Has No Attribute 'Placeholder'“. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *