Importing caffe results in ImportError: "No module named google.protobuf.internal" (import enum_type_wrapper)

52,328

Solution 1

This is probably because you have two python environments in your machine, the one provided by your linux distribution(pip) and the other by the anaconda environment (/home/username/anaconda2/bin/pip).

Try installing protobuf for both environments to be sure

pip install protobuf

/home/username/anaconda2/bin/pip install protobuf

Solution 2

If you are using Anaconda, do conda install protobuf

Solution 3

If you are using Ubuntu, try installing protobuf using

sudo apt-get install protobuf

It solved the same problem that I faced.

Solution 4

Easiest way to fix it:

pip install grpcio
pip install protobuf

When have multiple python version use: as suggested by aimuch.

/usr/local/bin/pip2 install protobuf
/usr/local/bin/pip2 install grpcio

Solution 5

This is because the python envirnment confusion.

# check where pip2
$ where pip2
/usr/local/bin/pip2
/usr/bin/pip2

# check where pip
$ which pip
/usr/local/bin/pip

On my computer, I have two pip2(I install caffe using python2 env), so I used /usr/local/bin/pip2 install protobuf solved this problem.

/usr/local/bin/pip2 install protobuf
Share:
52,328
PyNerd
Author by

PyNerd

Updated on January 28, 2020

Comments

  • PyNerd
    PyNerd over 4 years

    I installed Anaconda Python on my machine. When I start the Python Interpreter and type "import caffe" in the Python shell, I get the following error:

    ImportError: No module named google.protobuf.internal
    

    I have the following files:

    wire_format_lite_inl.h
    wire_format_lite.h
    wire_format.h
    unknown_field_set.h
    text_format.h
    service.h
    repeated_field.h
    reflection_ops.h
    message_lite.h
    message.h
    generated_message_util.h
    extension_set.h
    descriptor.proto
    descriptor.h
    generated_message_reflection.h
    generated_enum_reflection.h
    dynamic_message.h
    descriptor.pb.h
    descriptor_database.h
    

    What files do I need so the import will work? Is there an "internal.h" file that is required?

    • ypx
      ypx almost 8 years
      How did you install protobuf? via conda or apt-get or from source? Did you also install the protobuf python package?
    • PyNerd
      PyNerd almost 8 years
      Thank you for your response. I did not install protobuf at all. I will check our GitHub for the Protobuf Python package. I don't think it is there, but it will be worth a check. In case it is not, I requested Google Protobuf from the approved software list to be installed on the OS where I am working.
    • H S Rathore
      H S Rathore over 3 years
      @PyNerd, yeah you didn't install protobuf, tensorflow might have installed it, with a different version which is not working with caffe, So if you uninstall and install it again, it'll install latest version of protobuf which might not be compatible with tensorflow but the import caffe will succeed. Usually this happens if we try to install tensorflow with a specific version.
  • Guig
    Guig almost 8 years
    why do you need sudo?
  • Jayant Agrawal
    Jayant Agrawal over 7 years
    Thanks @Guig, sudo is not required. Edited
  • nkron
    nkron about 7 years
    This did not work for me, but sudo apt-get install python-protobuf did.
  • Unico
    Unico over 4 years
    pip uninstall protobuf pip install protobuf it worked for me.
  • Ammad
    Ammad over 4 years
    This solve it. /usr/local/bin/pip2 install protobuf
  • H S Rathore
    H S Rathore over 3 years
    yeah probably grpcio has nothing to do with the problem.