Attributeerror: module tensorflow has no attribute app error occurs because of version incompatibility between tensorflow versions. Actually, TesnorFlow community maintains two versions of TensorFlow 1. x series and 2. x series. There are too many syntax differences in both versions. Some are too drastic as well which is breaking the code as well like the above error. Few changes are impacting as a warning such as performance etc which we can ignore. Anyways in this article, we will explore the best way to fix this error.
This problem is easy to solve either you downgrade the interpreter’s tensorflow version or make your current code compatible with existing tensorflow version 2.0. The second one seams lengthy but more safe than first. Because if you downgrade the ternsorflow version to fix a single Attributeerror, You may end with tons of errors. Well, let’s address the solution differently.
Suppose your interpreter has tensorflow 2.x version install but you code entirely or subsection is on tensorflow 1.x then for that subsection you can import tensorflow as below –
import tensorflow.compat.v1 as tf
It will give backward compatibility.
If your complete code if fine and compatible with tensorFlow but somehow one or two lines are breaking because of this app attribute then only in this case, you should try
Correct –
tf.compat.v1.flags
In the place of ( incorrect )
tf.app.flags
Suppose one of your utilities is completely on tensorFlow 1. x and the rest of the project or utility is entirely in tensorFlow 2.x. In this case, you should try this trick. Since a major part of your code is in tensorflow 2.x, Hence we will convert the code to the 1. x oriented code in 2. x version.
tf_upgrade_v2 \
--intree code_1.x/ \
--outtree code_2.x/ \
--reportfile log.txt
Thanks
Data Science Learner Team