I believe the following block of code could be removed wherever they appear. It acually flows into the exception. try: # %tensorflow_version only exists in Colab. %tensorflow_version 2.x except Exception: print('This does not pass')