Cannot Import Name ‘builder’ from google.protobuf

This error means a package’s generated Protocol Buffers file is incompatible with the protobuf runtime Python is loading, or Python is loading the wrong copy of google.protobuf. The reliable fix is to install the package and its protobuf dependency together in a clean virtual environment, rather than upgrading or downgrading protobuf by itself. The builder in this message is an internal protobuf helper module; it is not the same thing as a command such as gftools builder.

Use this table to identify the likely cause before changing packages.

Symptom Likely Cause Safe Fix
A *_pb2.py file fails on from google.protobuf.internal import builder Generated protobuf code expects a different runtime than the one installed Reinstall the owning package and its dependencies in a clean environment
pip says packages are installed, but the import still fails pip installed into a different Python environment Use python -m pip with the same Python executable that runs the failing command
The reported path points into your project folder instead of site-packages A local google.py, google folder, or copied protobuf package is shadowing the real one Rename or remove the shadowing file or folder, then recreate the environment
pip check reports dependency conflicts The environment contains an unsatisfied package version requirement Reinstall from the project’s declared dependencies or update the package that ships the generated files

What the Protobuf builder Import Error Means

Protocol Buffers generates Python files with names such as example_pb2.py or fonts_public_pb2.py. Those generated files are Python source code, but they are tied to the protobuf runtime API available when they were generated.

A line like this in a generated file is the important clue:

from google.protobuf.internal import builder as _builder

google.protobuf.internal is protobuf’s implementation namespace. It is not a stable public API for application code. When Python says it cannot import builder, the generated file expects an internal helper that the installed protobuf runtime does not provide.

Treat generated _pb2.py files and the protobuf runtime as one matched dependency set, not as unrelated Python packages.

This often happens after one of these changes:

  • A package upgrade replaced generated *_pb2.py files but did not update protobuf.
  • An older package installation remains in a virtual environment.
  • A requirements file, lockfile, or constraint forces an incompatible protobuf release.
  • pip ran under one interpreter while the program ran under another.
  • A local file or directory named google changes Python’s import resolution.

How to Fix the Error in a Clean Environment

A fresh virtual environment installs the package that owns the failing *_pb2.py file and verifies the protobuf import.

Step 1: Confirm Which Python and Protobuf Installation Are Failing

Run these commands from the same terminal and project directory where the error occurs. If you normally run the program with python3, use python3 consistently instead of python.

python -c "import sys; print(sys.executable); print(sys.version)"
python -m pip --version

The Python path printed by the first command and the Python path shown by pip --version should refer to the same environment.

Next, find the protobuf package Python is actually importing.

python -c "import google.protobuf; print(google.protobuf.__file__)"

Then reproduce the narrow import failure directly.

python -c "from google.protobuf.internal import builder; print(builder.__file__)"

If this command fails, the issue is below the application layer: the active environment has an incompatible or shadowed protobuf installation.

Step 2: Create a New Virtual Environment

A fresh environment removes stale packages, leftover generated files, and interpreter confusion from the diagnosis.

On macOS or Linux:

python -m venv venv-protobuf

On Windows:

py -m venv venv-protobuf

Install the current package installer into that environment.

On macOS or Linux:

venv-protobuf/bin/python -m pip install --upgrade pip

On Windows:

venv-protobuf\Scripts\python.exe -m pip install --upgrade pip

I reproduce one dependency problem at a time in a clean virtual environment because it removes machine-specific noise.

Step 3: Install the Owning Package, Not Just protobuf

Install the project or tool that owns the failing *_pb2.py file. Let its dependency metadata select a compatible protobuf runtime.

For a Python project that has a pyproject.toml or setup.py, run this from the project root:

venv-protobuf/bin/python -m pip install .

For a project that explicitly uses a requirements file:

venv-protobuf/bin/python -m pip install -r requirements.txt

For the standalone Google Fonts tools package, install the tool itself:

venv-protobuf/bin/python -m pip install --upgrade gftools

On Windows, replace venv-protobuf/bin/python with venv-protobuf\Scripts\python.exe.

Avoid treating this as a command to blindly install the newest protobuf release:

pip install --upgrade protobuf

That can repair an environment when the runtime is simply too old, but it can also break an older package whose generated protobuf files require a different runtime. Installing the package that owns the generated file gives the dependency resolver the information it needs.

Step 4: Check the Resolved Dependencies and Retry the Import

Check whether the newly installed environment contains declared dependency conflicts.

venv-protobuf/bin/python -m pip check

A successful result reports that no broken requirements were found.

Test whether the active environment can import protobuf’s internal builder module:

venv-protobuf/bin/python -c "from google.protobuf.internal import builder; print(builder.__file__)"

If the failing project is Google Fonts Tools, test its generated protobuf module too.

venv-protobuf/bin/python -c "import gftools.fonts_public_pb2; print('gftools protobuf import OK')"

Finally, rerun the original build or command using this environment. For a shell-based gftools project with configuration files under sources, this runs each configuration through the newly installed command:

for config in sources/config*.yaml; do venv-protobuf/bin/gftools builder "$config"; done

If the direct protobuf import works but the project command still fails, read the new traceback from the top. It may expose a separate missing dependency or project-specific configuration error rather than the original protobuf failure.

When a Clean Reinstall Does Not Fix It

A Lockfile or Constraint Forces the Wrong Pair

A clean environment can faithfully reproduce the problem when the project’s own requirements lock an incompatible package combination. This is common when one dependency was upgraded independently while another dependency still assumes an older generated protobuf API.

Run the dependency check in the clean environment:

venv-protobuf/bin/python -m pip check

If it reports a conflict, do not override it by randomly pinning protobuf. Update the package that ships the failing *_pb2.py file, or restore the project’s known working dependency lock. The package that generated or distributes the protobuf file and the protobuf runtime need compatible releases.

Python Is Importing a Local google Package

A project file named google.py or a project directory named google can override the installed google.protobuf namespace. Copied dependency directories can cause the same issue.

The path from this command should normally point into the active environment’s site-packages directory:

venv-protobuf/bin/python -c "import google.protobuf; print(google.protobuf.__file__)"

If it points into your repository, rename the local conflicting file or directory, remove its compiled cache files, and recreate the virtual environment. Do not delete a legitimate installed namespace package merely because its top-level name is google; verify the exact import path first.

Your Project Owns the Generated *_pb2.py File

If the failing *_pb2.py file is checked into your own repository, the long-term fix is to regenerate it using the project’s supported protobuf compiler and declare the matching runtime dependency in the project metadata.

Do not manually remove the builder import from generated code. It may make one import succeed while leaving descriptors, messages, or serialization behavior inconsistent later. Regenerate the file or update the source package that generated it.

The Program Runs in Docker or Another Environment

Installing packages on the host does not repair a container, CI worker, notebook kernel, or remote interpreter. Run the Python-path and protobuf-path checks inside the environment that actually executes the failing command, then rebuild or recreate that environment from its dependency files.

Confirm the Repair and Prevent It Returning

A repaired environment should pass all three checks:

  • python -m pip check reports no broken requirements.
  • from google.protobuf.internal import builder imports without an exception.
  • The original application command runs past the prior *_pb2.py traceback.

Use the interpreter-qualified form of pip for future changes:

python -m pip install package-name

This prevents a common mismatch where pip modifies one Python installation while python runs another.

For projects that distribute generated protobuf code, keep the protobuf compiler version, generated files, and runtime requirement under version control through the project’s normal build configuration. For applications that merely consume those files, upgrade or reinstall the package that owns them instead of importing or depending on google.protobuf.internal directly.

Frequently Asked Questions

Does upgrading protobuf always fix this error?

No. Upgrading protobuf can help when generated code requires a newer runtime that provides google.protobuf.internal.builder, but it can break packages generated for an older runtime. Install or update the package that owns the failing *_pb2.py file so its dependency constraints select the intended protobuf version.

Is google.protobuf.internal.builder the same as gftools builder?

No. google.protobuf.internal.builder is a Python module used internally by generated protobuf code. gftools builder is a Google Fonts Tools command for building font projects; the name overlap is coincidental.

Can I edit the generated _pb2.py file to remove the import?

No. Editing generated protobuf code is not a durable fix and can leave the file incompatible with the runtime in less obvious ways. Reinstall or update the package that provides the file, or regenerate the file from its .proto source under the project’s supported protobuf toolchain.

Why does the error happen only on one computer or in one virtual environment?

The environments likely resolve different package versions, use different Python executables, or import protobuf from different locations. Checking sys.executable, python -m pip --version, and google.protobuf.__file__ in the failing environment identifies which installation is actually being used.

What should I do if gftools still fails after reinstalling it?

Confirm that the traceback still names gftools/fonts_public_pb2.py, then recreate the environment from the project’s declared dependencies rather than mixing global and virtual-environment installs. The failing import in the reported gftools issue comes from fonts_public_pb2.py importing protobuf’s internal builder; compare its paths with your traceback, then update the project package set together.