AttributeError: module ‘importlib’ has no attribute ‘util’ when install Google Cloud SDK on macOS

Was getting the following error on macOS when installing Google Cloud SDK:

I suspected it’s because I had Python 3.9 installed and that was being used (notice it says python@3.9 in the errors) but Google Cloud SDK only supports up to Python 3.8.

How to get brew to use Python 3.8 just for this install though? Wasn’t sure. I tried being sneaky and make aliases pointing python3 and python3-config to point to the python@3.8 installed versions but that didn’t help. I couldn’t uninstalled the python@3.9 package either as it was required by others.

I downloaded the tar ball from the Google Cloud website and tried a manual install with the same results. Then I noticed the install.sh script mentions a CLOUDSDK_PYTHON variable to specify the Python version. So I tried CLOUDSDK=python3.8 ./install.sh and voila! that worked.

After successfully installing I had further trouble running the gcloud command as it gave the same error. So I added export CLOUDSDK_PYTHON=python3.8 to my .bash_profile so the cloud SDK always uses Python 3.8. That helped.