Generated _pb2.py files fail to import against an older protobuf
Your error
ImportError: cannot import name 'runtime_version' from 'google.protobuf'1 more line it can showFix it fast
Most common cause, 1 of 2The runtime is too old to have the runtime_version module at all
The clean fix is a runtime that is new enough.
# option A: newer stack
pip install -U "tensorflow==2.21.0" "protobuf>=6.31.1"
# option B: keep protobuf 4.x, rebuild the gencode with protoc 25.9
curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v25.9/protoc-25.9-linux-x86_64.zip
unzip -o protoc-25.9-linux-x86_64.zip -d "$HOME/protoc25"
"$HOME/protoc25/bin/protoc" object_detection/protos/*.proto --python_out=.Short answer
ImportError: cannot import name 'runtime_version' from 'google.protobuf' means your _pb2.py files came from protoc 27 or newer, while the installed protobuf package predates that module (4.x or early 5.x). Newer generated code on an older runtime is never supported. Upgrade protobuf to at least the generated version, or regenerate the files with a protoc that matches your runtime.
Which cause do you have?
Print the installed runtime, the protoc on your PATH, and the gencode version written inside one failing _pb2.py file. A runtime below 5.27 explains the ImportError (cause 1). A runtime of 5.27 or later that is still below the gencode numbers explains the VersionError (cause 2).
python -c "import google.protobuf as p; print(p.__version__)"
protoc --version
grep -m1 -A6 "ValidateProtobufRuntimeVersion" object_detection/protos/string_int_label_map_pb2.pyThen find the line you see in this list and open its fix:
- 1The runtime is too old to have the runtime_version module at all
python prints protobuf 4.x or 3.x, and the _pb2.py file starts with from google.protobuf import runtime_version as _runtime_version - 2The runtime has runtime_version but is older than the gencode
The traceback ends in VersionError and names both numbers, for example gencode 6.31.1 runtime 5.29.3
What does the error look like?
ImportError: cannot import name 'runtime_version' from 'google.protobuf'google.protobuf.runtime_version.VersionError: Detected incompatible Protobuf Gencode/Runtime versions when loading object_detection/protos/string_int_label_map.proto: gencode 6.31.1 runtime 5.29.3. Runtime version cannot be older than the linked gencode version.
Cause 1 of 2
The runtime is too old to have the runtime_version module at all
You seepython prints protobuf 4.x or 3.x, and the _pb2.py file starts with from google.protobuf import runtime_version as _runtime_version
Check
The ImportError for runtime_version means a protobuf runtime older than 5.27 is loading code from protoc 27 or later. From release 27.0 the Python generator writes that import plus a ValidateProtobufRuntimeVersion call into every _pb2.py, and the module only exists in runtimes from the same release on. TensorFlow 2.15 to 2.17 cap protobuf below 5, so compiling protos with a current protoc next to them produces exactly this.
pip show protobuf tensorflow | grep -E "^(Name|Version)"Fix
The clean fix is a runtime that is new enough. TensorFlow 2.21 already requires protobuf 6.31.1 or later, so upgrading TensorFlow and protobuf together removes the cap. If you must keep an old TensorFlow, regenerate the files with protoc 25.x, the last compiler line for protobuf Python 4.x.
# option A: newer stack
pip install -U "tensorflow==2.21.0" "protobuf>=6.31.1"
# option B: keep protobuf 4.x, rebuild the gencode with protoc 25.9
curl -LO https://github.com/protocolbuffers/protobuf/releases/download/v25.9/protoc-25.9-linux-x86_64.zip
unzip -o protoc-25.9-linux-x86_64.zip -d "$HOME/protoc25"
"$HOME/protoc25/bin/protoc" object_detection/protos/*.proto --python_out=.Verify
Importing the generated module succeeds without any traceback.
python -c "from object_detection.protos import string_int_label_map_pb2; print('ok')"Confirmed by raw.githubusercontent.com
Cause 2 of 2
The runtime has runtime_version but is older than the gencode
You seeThe traceback ends in VersionError and names both numbers, for example gencode 6.31.1 runtime 5.29.3
Check
Here the module exists, so the import works, and the check inside it refuses the pairing. Protobuf's cross-version guarantee says generated code must never run on a runtime that predates the protoc that produced it. runtime_version.py raises VersionError whenever the runtime version, compared as major, then minor, then patch, comes out lower than the gencode version.
python -c "import google.protobuf as p; print(p.__version__)"Fix
Install a runtime at least as new as the gencode version from the message, as long as your other packages allow it. If a pin blocks that, regenerate with a protoc whose Python version is not newer than your runtime; protoc 26 to 29 map to Python 5.x, 30 to 33 to 6.x, and 34 onward to 7.x.
pip install -U "protobuf>=6.31.1"
pip checkVerify
pip check reports no broken requirements and the failing import now loads.
python -c "from object_detection.protos import string_int_label_map_pb2"Confirmed by protobuf.dev
How do I stop it coming back?
- Pin the protoc or grpcio-tools version in the repo next to the protobuf pin, and generate code in CI with exactly that compiler.
- Never compile protos with whatever protoc the OS package manager installed; record protoc --version in the build log.
- Commit the _pb2.py files, or rebuild them in the same Docker image that runs the code, so runtime and gencode cannot drift apart.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.