Protobuf Python defaults to C++ implementation?

981 views
Skip to first unread message

Bu Sun Kim

unread,
Apr 22, 2021, 4:25:09 PM4/22/21
to Protocol Buffers

Hi,

 https://developers.google.com/protocol-buffers/docs/reference/python-generated#cpp_impl states that the default implementation is Python and that one needs to opt in to C++ by setting PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION.

However, when I install the latest version of protobuf without setting that environment variable I still seem to get the C++ implementation:


busunkim@busunkim:~$ python3 -m venv protobufenv

busunkim@busunkim:~$ source protobufenv/bin/activate

(protobufenv) busunkim@busunkim:~$ python3 -m pip install protobuf

Collecting protobuf

  Using cached https://files.pythonhosted.org/packages/35/0b/e39294cc56f54dd61d45b58668b0d78ec67bce7fead7baec7f01bebf498c/protobuf-3.15.8-cp38-cp38-manylinux1_x86_64.whl

Collecting six>=1.9 (from protobuf)

  Using cached https://files.pythonhosted.org/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl

Installing collected packages: six, protobuf

Successfully installed protobuf-3.15.8 six-1.15.0

WARNING: You are using pip version 19.2.3, however version 21.0.1 is available.

You should consider upgrading via the 'pip install --upgrade pip' command.

(protobufenv) busunkim@busunkim:~$ python3

Python 3.8.3 (default, Jun 15 2020, 16:29:21) 

[GCC 9.3.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> from google.protobuf.internal import api_implementation

>>> api_implementation.Type()

'cpp'

 

Under what conditions does someone end up with the C++ implementation vs. the Python implementation? Is it possible to know in which version that change was made?


Joel Holveck

unread,
Apr 24, 2021, 4:55:09 AM4/24/21
to Protocol Buffers
Pip is installing a prebuilt binary.  That's why the filename ends in "-manylinux1_x86_64.whl"; it's a prebuilt binary for Linux on x86/64.  The comment in the docs indicate you'd need to set it that environment variable at build time.  At the least, you'll need to build Protobuf from source to get the Python implementation.
Reply all
Reply to author
Forward
0 new messages