9

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?

IKavanagh
  • 6,089
  • 11
  • 42
  • 47
Greg
  • 10,696
  • 22
  • 68
  • 98
  • I'm not a pytthon guy, but have you included the Thrift library code as well? There are two parts, the generated code in `gen-py` and the library code. – JensG Nov 11 '14 at 17:08

4 Answers4

6

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.

Greg
  • 10,696
  • 22
  • 68
  • 98
2

I also came across this problem on my macOS Mojave 10.14.1. And it seems the reason is that --with-python will install python@2 and the thrift module inside it. However, you are using the system python, which is different from the python@2. You may verify this by brew list | grep python and you should see the following result.

python
python@2

After removing python@2 (brew uninstall --ignore-dependencies python@2) and installing the python module from the source should work.

git clone https://github.com/apache/thrift.git
cd thrift/lib/py
sudo python setup.py install

For more details about the cause, you may refer to this post.

Jianchao Li
  • 313
  • 1
  • 5
  • 9
2

Removing python@2 (brew uninstall --ignore-dependencies python@2) did not work for me. In fact, I had to reinstall brew install python@2

pip install thrift worked for me.

My system was OSX Mojave and python 2.7. brew list | grep python had following result: python python@2

Test Test
  • 145
  • 1
  • 14
2

I am not using Mac / Homebrew, but here is how I solved this issue:

I am using Linux, and my Thrift server ran fine in PyCharm, but I was getting the ModuleNotFoundError: No module named 'thrift' error when running from the command line.

It turned out that although I had Thrift installed on my system, I had not installed it using Pip. Once I ran:

pip install thrift

... Everything worked fine.

JoeMjr2
  • 3,804
  • 4
  • 34
  • 62