先导入matplotlib再导入tensorflow触发Protocol Buffer版本错误的原因及同类场景咨询
Protocol Buffer Version Conflict When Importing matplotlib.pyplot Before TensorFlow
Error Context
When importing
matplotlib.pyplotbeforetensorflow, you'll hit this fatal protobuf version error:~$ python Python 3.5.2 (default, Oct 8 2019, 13:06:37) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import matplotlib.pyplot >>> import tensorflow [libprotobuf FATAL google/protobuf/stubs/common.cc:61] This program requires version 3.8.0 of the Protocol Buffer runtime library, but the installed version is 2.6.1. Please update your library. If you compiled the program yourself, make sure that your headers are from the same version of Protocol Buffers as your link-time library. (Version verification failed in "bazel-out/k8-opt/genfiles/ tensorflow/core/framework/tensor_shape.pb.cc".) terminate called after throwing an instance of 'google::protobuf::FatalException' what(): This program requires version 3.8.0 of the Protocol Buffer runtime library, but the installed version is 2.6.1. Please update your library. If you compiled the program yourself, make sure that your headers are from the same version of Protocol Buffers as your link-time library. (Version verification failed in "bazel-out/k8-opt/genfiles/ tensorflow/core/framework/tensor_shape.pb.cc".) Abandon (core dumped)Reversing the import order (tensorflow first, then matplotlib) works without issues.
Root Cause of the Error
This is entirely about dynamic shared library loading order and conflicting Protocol Buffer (protobuf) versions:
matplotlib.pyplot(or one of its underlying dependencies, like certain backend components) links against an older system-wide protobuf library (2.6.1 here). When you import it first, Python loads this old shared library into memory, and once loaded, it can't be unloaded or replaced.- TensorFlow requires a much newer protobuf version (3.8.0). When it tries to initialize after the old library is already present, it has to use the existing 2.6.1 version—this fails protobuf's strict version validation check, triggering the fatal error and core dump.
When you reverse the order:
- TensorFlow loads its required 3.8.0 protobuf library first. Since
matplotliband its dependencies are compatible with this newer version (they don't enforce a strict requirement for the old 2.6.1), they can work seamlessly with the already-loaded library, avoiding conflicts.
Similar Scenarios Where This Might Occur
This is a common pitfall with Python libraries that rely on shared C/C++ dependencies. You might run into identical issues in these cases:
- Conflicting CUDA/CuDNN versions: Libraries like OpenCV (linked to an older CUDA runtime) being imported before TensorFlow/PyTorch (which needs a newer CUDA version).
- Mixed package manager installs: Using both
pipandcondato install libraries that share underlying dependencies (e.g.,numpy,scipy), leading to mismatched shared library versions. - System vs. virtual environment conflicts: Importing a system-wide installed library (linked to outdated dependencies) before a virtual environment library that requires newer versions.
- Scientific framework clashes: Libraries like
scikit-learn(depending on older linear algebra libraries) conflicting with TensorFlow/PyTorch's updated linear algebra dependencies.
内容的提问来源于stack exchange,提问作者nish-ant




