Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module named thrift in Python script

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?

like image 763
Greg Avatar asked Nov 11 '14 15:11

Greg


2 Answers

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.

like image 107
Greg Avatar answered Sep 17 '22 11:09

Greg


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.

like image 33
Jianchao Li Avatar answered Sep 17 '22 11:09

Jianchao Li