No module named thrift in Python script

Greg picture Greg · Nov 11, 2014 · Viewed 15.3k times · Source

I've installed Thrift on my Mac using Homebrew: brew install thrift --with-python

That did some work and finished w/o errors reported. I have thrift on my path.

I write a simple python client from a tutorial: (there is some python thrift code in gen-py)

#!/usr/bin/python

import sys
sys.path.append("./gen-py")

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

# rest of program...

When run I get this error:

Traceback (most recent call last):
  File "./hey.py", line 8, in <module>
    from thrift import Thrift
ImportError: No module named thrift

Did I installed python correctly using Homebrew? Is there a way I can verify the python/thrift integration is installed properly?

Answer

Greg picture Greg · Nov 12, 2014

Turns out the (current at time of this writing) Homebrew recipe for Thrift installation doesn't fully install everything for python, specifically the libraries. (It does successfully install the thrift command-line tools.)

I fixed this by downloading the python distribution and building it on my Mac--but not installing since I'd already done a brew install. I just wanted the python libs.

I followed advice here: http://thrift-tutorial.readthedocs.org/en/latest/usage-example.html I ran "sudo python setup.py install" as described and this correctly (apparently) installed the needed libs and things worked after that.