Resolve issue ImportError: No module named thrift.Thrift when running python script

 

When running python script which using struct and/or service defined in thrift, a problem shows up with the error message below:

 
Traceback (most recent call last):
   File "./test.py", line 8, in <module>
     from test import TestService
   File "../thrift/gen-py/test/TestService.py", line 9, in <module>
     from thrift.Thrift import TType, TMessageType, TException, TApplicationException
   ImportError: No module named thrift.Thrift

From the error message we can see that the python script test.py import a module TestService which is defined in a thrift and the thrift-generated python file is "../thrift/gen-py/test/TestService.py, and the file TestService.py imports TType, TMessageType, TException, TApplicationException and get ImportError.

It must be a configuration issue since TType, TMessageType, TException, and TApplicationException should be included when Thrift is installed.

After searching around in Google, I found that a blog from here mentioned the similar error and provided a fix by going to the directory lib/py on the thrift installation directory and run the following command to install the thrift module to your python libraries:

sudo python setup.py install

Yeah, after running the command above, the issue is gone. 

Comments

Popular posts from this blog

Linux kernel documentation for /sys/class/net

VMware Tools