TensorFlow fails to import

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.

    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.

    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.

Note

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