Skip to content

ImportError: cannot import name 'builder' from 'google.protobuf.internal'

Problem Statement

When working with TensorFlow Object Detection API or other protobuf-related Python projects, you may encounter the following error:

ImportError: cannot import name 'builder' from 'google.protobuf.internal' 
(C:\Users\user\env\lib\site-packages\google\protobuf\internal\__init__.py)

This error typically occurs when there's a version mismatch between your protobuf installation and the protobuf compiler (protoc), or when the builder.py file is missing from your protobuf installation. The issue is particularly common with TensorFlow Object Detection API installations.

Root Cause

The error stems from architectural changes made in protobuf version 3.20.0. As stated in the release notes:

Protobuf python generated codes are simplified. Descriptors and message classes' definitions are now dynamically created in internal/builder.py. Insertion Points for message classes are discarded.

This means newer versions of protobuf-generated code expect to find a builder module that may be missing in older installations or certain distribution methods.

Solutions

The simplest solution is to upgrade your protobuf package to the latest version:

bash
pip install --upgrade protobuf

This ensures you have the builder.py module that newer protobuf-generated code requires.

Solution 2: Reinstall Protobuf

If upgrading doesn't work, try a clean reinstall:

bash
pip uninstall protobuf
pip install protobuf

Solution 3: Downgrade to Compatible Version

Some TensorFlow versions work best with specific protobuf versions. The most commonly recommended compatible version is 3.20.x:

bash
pip uninstall protobuf
pip install protobuf==3.20.3

Solution 4: Manually Add builder.py File

If you need to maintain a specific protobuf version, you can manually add the missing file:

  1. First, download the builder.py file:
bash
wget https://raw.githubusercontent.com/protocolbuffers/protobuf/main/python/google/protobuf/internal/builder.py
  1. Then copy it to your protobuf installation directory:
bash
# Typical path on Linux/Mac
cp builder.py /path/to/your/env/lib/python3.x/site-packages/google/protobuf/internal/

# Typical path on Windows
copy builder.py C:\Users\user\env\Lib\site-packages\google\protobuf\internal\

Solution 5: Ensure Protoc Compatibility

Make sure your protoc compiler version matches or is older than your protobuf Python package version:

  1. Check your protoc version:
bash
protoc --version
  1. Download compatible protoc versions from the official releases page if needed.

Solution 6: Conda Environment Fix

If using Conda, try installing protobuf through conda instead of pip:

bash
pip uninstall protobuf
conda install protobuf

Best Practices

Version Compatibility

Always check TensorFlow's documentation for recommended protobuf versions. The TensorFlow team typically tests with specific versions and documents compatibility requirements.

Multiple Installations

Avoid having both pip and conda installations of protobuf in the same environment, as this can cause conflicts. Use one package manager consistently.

Verification

After applying any solution, verify the fix by running:

python
python -c "from google.protobuf.internal import builder; print('Import successful')"

If this command executes without errors, the issue has been resolved.

Conclusion

The "cannot import name 'builder'" error is primarily a version compatibility issue between protobuf components. The solutions range from simple package upgrades to manual file additions. For most users, upgrading to the latest protobuf version (Solution 1) will resolve the issue. For TensorFlow-specific cases, using version 3.20.x (Solution 3) often provides the best compatibility.