How can I use protocol buffers for Python on windows?

25,745

How can I correctly install and run protocol buffers from Python on Windows?

Like any other package, you have to actually install it if you want it to be installed. If you just try to run with the package sitting in your source directory, it might work, but most packages don't work that way; you tend to get things like the top-level package importing and then failing a few lines down when it tries to import something else… exactly as you're seeing.

I believe an installable package comes in the main download package from GoogleCode. At least it does for the source packages, if not the win32 package. And inside the python directory are complete instructions for installing it. Basically:

C:\path\to\protobuf-2.5.0> cd python
C:\path\to\protobuf-2.5.0\python> python setup.py build
C:\path\to\protobuf-2.5.0\python> python setup.py test
C:\path\to\protobuf-2.5.0\python> python setup.py install

But if that doesn't come with the win32 pre-built package, or you don't have it lying around anymore, or you just prefer to install off PyPI, it's also available there. So, assuming you've got pip installed:

pip install protobuf
Share:
25,745
user2399973
Author by

user2399973

Updated on July 09, 2022

Comments

  • user2399973
    user2399973 almost 2 years

    I have been trying to use protocol buffers in my Python program, but cannot get it to work. I'm running a Windows 8 machine and have tried Python 2.7.6 and Python 3.3. I downloaded the binary protocol buffer compiler for Python and used it to generate myProto_pb2.py from my myProto.proto file, but when I get the following error when I run my Python program:

    from the "import myProto_pb2" line, I get the following error when using Python 2.7.6 from protocol buffers version 2.5:

    from google.protobuf import descriptor as _descriptor
      ImportError: No module named google.protobuf
    

    How can I correctly install and run protocol buffers from Python on Windows?

  • user2399973
    user2399973 over 10 years
    Thank you! It appears to work... for some reason, I can run my Python program from a command prompt like Cygwin, but I still get the import errors when launching it from the default Idle editor.
  • abarnert
    abarnert over 10 years
    @user2399973: My guess is that you've actually installed two Python installations—maybe a cygwin Python and a native win32 Python. You've correctly installed protobuf into the cygwin Python, so it works when you run the cygwin Python at the command line, but you haven't installed it into the native Python, so it doesn't work when you run the native Python's IDLE. But really, that's just a guess. Post a new question, describe your setup, show the actual error message (with traceback, if any) rather than just describing it, etc.