34

I am trying to run Google's deep dream. For some odd reason I keep getting

ImportError: No module named google.protobuf

after trying to import protobuf. I have installed protobuf using sudo install protobuf. I am running python 2.7 OSX Yosemite 10.10.3.

I think it may be a deployment location issue but i cant find anything on the web about it. Currently deploying to /usr/local/lib/python2.7/site-packages.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
lecorbu
  • 343
  • 1
  • 3
  • 7
  • 1
    Did you pip install google.protobuf? – Oren Haliva Jul 09 '15 at 05:28
  • I did "pip install protobuf" but I still get ImportError: No module named google.protobuf after using "from google.protobuf import text_format" in python 2.7.6 – lecorbu Jul 09 '15 at 05:47
  • Is there a chance you have two pythons on your computer? If you do use the pip directly from scripts folder – Oren Haliva Jul 09 '15 at 05:59
  • I think you are on to something. I do have multiple python versions. What do you mean by using the pp directly from the scripts folder? – lecorbu Jul 09 '15 at 17:39

9 Answers9

36

There is another possibility, if you are running a python 2.7.11 or other similar versions,

sudo pip install protobuf

is ok.

But if you are in a anaconda environment, you should use

conda install protobuf
empty
  • 5,194
  • 3
  • 32
  • 58
Panfeng Li
  • 3,321
  • 3
  • 26
  • 34
  • 1
    What if I have compiled protobuf 2.6.0 source and the python is 3.5? I am facing an issue for importing caffe `from .proto.caffe_pb2 import TRAIN, TEST File "/usr/local/lib/python3.5/dist-packages/caffe/proto/caffe_pb2.py", line 6, in from google.protobuf.internal import enum_type_wrapper ImportError: No module named 'google.protobuf'`. – S.EB Jul 17 '18 at 15:56
  • 1
    I have exported the path in bashrc as well `LD_LIBRARY_PATH=/usr/local/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}`, and I do not know what is the reason – S.EB Jul 17 '18 at 15:57
  • why do I have to do `conda install` with this particular package instead of `pip install` that works with other packages? – rvictordelta Oct 22 '18 at 13:01
  • @S.EB I am in a similar situation with Protobuf 2.6.1, from what I gather from my testing, the compiler itself isn't backwards compatible (as in, protoc from Protobuf 3.x.x can't compile .proto files created with Protobuf 2 in mind), but the library API (including the Python interface) is backwards compatible. I was able to successfully use the latest protobuf package from pip to communicate with a device that uses an interface designed for Protobuf 2. – jrh Jun 06 '19 at 17:50
  • @S.EB Also, don't try and use the python interface that comes with Protobuf 2.6.X, it seems to be made for Python 2 and it won't work with any Python 3 scripts; I've found some posts / commit messages saying that Google added support for Python 3 in 2.6.0, but I don't think it was finished at that time, because the code seems to only support a Python version of 1 or 2 for the setup, 3 isn't accepted. – jrh Jun 06 '19 at 17:51
21

Locating the google directory in the site-packages directory (for the proper latter directory, of course) and manually creating an (empty) __init__.py resolved this issue for me.

(Note that within this directory is the protobuf directory but my installation of Python 2.7 did not accept the new-style packages so the __init__.py was required, even if empty, to identify the folder as a package folder.)

...In case this helps anyone in the future.

Dan Nissenbaum
  • 13,558
  • 21
  • 105
  • 181
10

In my case I

  1. downloaded the source code, compiled and installed:

    $ ./configure
    $ make
    $ make check
    $ sudo make install`
    
  2. for python I located its folder(python) under source code, and ran commands:

    $ python setup.py build
    $ python setup.py install'
    

Not sure if this could help you..

iSean
  • 139
  • 1
  • 6
  • I'd like to add that for some systems step 1 is probably already done for you. You just need to do step 2. In my case I had to dowload the whole protobuf zip file and look for the python subfolder. – Wilmer E. Henao Dec 19 '17 at 21:00
  • 2
    Wanted to add that the repository has changed to now require ./autogen.sh before you can run ./configure. – tbranyen Apr 26 '18 at 16:56
8

I got the same error message when I tried to use Tensor Flow. The solution was simply to uninstall Tensor Flow and protobuf:

$ sudo pip uninstall protobuf
$ sudo pip uninstall tensorflow

And reinstall it again: pip installation of Tensorflow. Currently, this is:

# Ubuntu/Linux 64-bit, CPU only:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl

# Mac OS X, CPU only:
$ sudo easy_install --upgrade six
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0rc0-py2-none-any.whl
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
3

when I command pip install protobuf, I get the error:

Cannot uninstall 'six'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

If you have the same problem as me, you should do the following commands.

pip install --ignore-installed six sudo pip install protobuf

leon wu
  • 315
  • 3
  • 10
2

According to your comments, you have multiply versions of python what could happend is that you install the package with pip of anthor python

pip is actually link to script that donwload and install your package.

two possible solutions:

  1. go to $(PYTHONPATH)/Scripts and run pip from that folder that way you insure you use the correct pip
  2. create alias to pip which points to $(PYTHONPATH)/Scripts/pip and then run pip install

how will you know it worked? Simple if the new pip is used the package will be install successfully, otherwise the package is already installed

Oren Haliva
  • 351
  • 3
  • 14
0

I installed the protobuf with this command:

conda install -c anaconda protobuf=2.6.1

(you should check the version of protobuf)

neouyghur
  • 1,577
  • 16
  • 31
0

In my case, MacOS has the permission control. sudo -H pip3 install protobuf

0

I had this problem to when I had a google.py file in my project files.
It is quite easy to reproduce.
main.py: import tensorflow as tf
google.py: print("Protobuf error due to google.py")

Not sure if this is a bug and where to report it.

blpasd
  • 409
  • 7
  • 18