I'm trying to use the gsutil
command line tool to create a bucket, and I'm getting the following error: ImportError: cannot import name json_format
(full stack trace below). Even running a simple gsutil --help
causes this.
I didn't have any such issues when using gsutil
, and I can't remember any specific changes in my environment that could cause this error. All I remember was updating gcloud components
to 201.0.0 before seeing this error. Anyway, I followed this suggestion on Stack Overflow to revert to gcloud components version 175.0.0 by running gcloud components update --version 175.0.0
and this error goes anyway.
When will we be able to use gustil
in gcloud components
version 201.0.0? Or is there some configuration that I'm missing?
Traceback (most recent call last):
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin/bootstrapping/gsutil.py", line 12, in <module>
import bootstrapping
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin/bootstrapping/bootstrapping.py", line 19, in <module>
from googlecloudsdk.core.credentials import store as c_store
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/credentials/store.py", line 31, in <module>
from googlecloudsdk.core import http
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/http.py", line 32, in <module>
from googlecloudsdk.core.resource import session_capturer
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/resource/session_capturer.py", line 34, in <module>
from googlecloudsdk.core.resource import yaml_printer
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/resource/yaml_printer.py", line 21, in <module>
from googlecloudsdk.core.resource import resource_printer_base
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/resource/resource_printer_base.py", line 42, in <module>
from googlecloudsdk.core.resource import resource_projector
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/resource/resource_projector.py", line 40, in <module>
from google.protobuf import json_format as protobuf_encoding
ImportError: cannot import name json_format
Traceback (most recent call last):
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin/bootstrapping/gsutil.py", line 12, in <module>
import bootstrapping
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin/bootstrapping/bootstrapping.py", line 19, in <module>
from googlecloudsdk.core.credentials import store as c_store
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/credentials/store.py", line 31, in <module>
from googlecloudsdk.core import http
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/http.py", line 32, in <module>
from googlecloudsdk.core.resource import session_capturer
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/resource/session_capturer.py", line 34, in <module>
from googlecloudsdk.core.resource import yaml_printer
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/resource/yaml_printer.py", line 21, in <module>
from googlecloudsdk.core.resource import resource_printer_base
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/resource/resource_printer_base.py", line 42, in <module>
from googlecloudsdk.core.resource import resource_projector
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/lib/googlecloudsdk/core/resource/resource_projector.py", line 40, in <module>
from google.protobuf import json_format as protobuf_encoding
ImportError: cannot import name json_format
Thanks and cheers!
Looks like several versions ago, the Cloud SDK team added the google/protobuf package [1] into their bootstrapping scripts (used to launch gsutil and other tools). It seems that if your system has another / an older version of that package, it can take precedence over the verison gcloud includes with its bundle, causing this import error. If you have this module installed in your system python installation, try uninstalling it so that gcloud's version will be used. If you're on macOS and using homebrew, running brew unlink protobuf@[YOUR INSTALLED VERSION]
should work.
Similar to what @gouglum said above, on a quite old VM my system actually had protobuf installed in /usr/local/lib/python2.7/dist-pacakges
. Upgrading gcloud didn't upgrade this protobuf, so I had to resort to running: sudo pip install protobuf --upgrade
which solved the issue.
I'm not sure if it's because I was upgrading from a very old gcloud (149) or if I somehow actually had installed protobuf into system packages earlier in the life of the VM.
This effectively breaks gsutil on RHEL or Centos7. Is there any other to fix this other than removing the python-protobuf package? That package is needed on my systems for other purposes to removing it is not an option.
Most helpful comment
Similar to what @gouglum said above, on a quite old VM my system actually had protobuf installed in
/usr/local/lib/python2.7/dist-pacakges
. Upgrading gcloud didn't upgrade this protobuf, so I had to resort to running:sudo pip install protobuf --upgrade
which solved the issue.I'm not sure if it's because I was upgrading from a very old gcloud (149) or if I somehow actually had installed protobuf into system packages earlier in the life of the VM.