TensorFlow fails to import

TensorFlow fails to import if you have an incompatible version of protobuf installed on your cluster.

Written by kavya.parag

Last published at: May 16th, 2022

Problem

You have TensorFlow installed on your cluster.

When you try to import TensorFlow, it fails with an Invalid Syntax or import error.

Cause

The version of protobuf installed on your cluster is not compatible with your version of TensorFlow.

Solution

Use a cluster-scoped init script to install TensorFlow with matching versions of NumPy and protobuf.

  1. Create the init script.
    %python
    
    dbutils.fs.put("/databricks/<init-script-folder>/install-tensorflow.sh","""
    #!/bin/bash
    set -e
    /databricks/python/bin/python -V
    /databricks/python/bin/pip install tensorflow protobuf==3.17.3 numpy==1.15.0
    """, True)
  2. Install the init script that you just created as a cluster-scoped init script (AWS | AzureGCP).
    You will need the full path to the location of the script (dbfs:/databricks/<init-script-folder>/install-tensorflow.sh).
  3. Restart the cluster after you have installed the init script.
Delete

Info

Uninstall all existing versions of NumPy before installing the init script on your cluster.