try:
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
Note: that %tensorflow_version
is only available in Colab and not in regular Python.
Note: This was useful when TensorFlow 1 was the default. But now TensorFlow 2 is available in Colab by default.