My symptom was trying to run a script that imported Thrift, receiving this message:
Traceback (most recent call last):However that module file exists in my disk: /usr/lib/python2.6/site-packages/thrift/transport/TTransport.py
File "./Cassandra-remote", line 11, in <module>
from thrift.transport import TTransport
ImportError: No module named thrift.transport
It took a while to realize that this path was not being inspected by python:
$ python(note that "site-packages" is not in the list). I assume that I configured my thrift code when the paths were different in my system, and some upgrade changed python directories. So when I "make install" thrift, they are still copied into site-packages. Now, having thrift already installed there, it is a matter of adding this dir to the search path. This is done with sys.path.append, according to python's documentation:
Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['',
'/usr/lib/python2.6',
'/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk',
'/usr/lib/python2.6/lib-old',
'/usr/lib/python2.6/lib-dynload',
'/usr/lib/python2.6/dist-packages',
'/usr/lib/python2.6/dist-packages/Numeric',
'/usr/lib/python2.6/dist-packages/PIL',
'/var/lib/python-support/python2.6',
'/var/lib/python-support/python2.6/gtk-2.0',
'/usr/local/lib/python2.6/dist-packages']
>>> sys.path.append('/usr/lib/python2.6/site-packages')
>>> sys.path
['',
'/usr/lib/python2.6',
(...)
'/usr/local/lib/python2.6/dist-packages',
'/usr/lib/python2.6/site-packages']
>>> from thrift.transport import TTransport
(No error message now)
The "sys.path.append" worked, by I need to persist this change. This can be done by changing the variable PYTHONPATH, I am adding this line to my .bashrc:
export PYTHONPATH=/usr/lib/python2.6/site-packages
And that's it. Any new console (on my user at least) gets this path and I can import the thrift module now.
I wonder how to make this change available to all users -- where is the "default" PYTHONPATH defined?
Wonderful! Thanks for taking the time to put this up. :) helped me out.
ReplyDelete+1 ty
ReplyDeleteThank you very much :)
ReplyDeletethanks
ReplyDeleteThanks
ReplyDeleteplease help it is not working...)
ReplyDeleteThanks a lot...Tried your steps and acted like magic!!! :)
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks for the $PYTHONPATH settings, it worked for me with python 2.7.
ReplyDeletethanks a lot!
ReplyDelete谢谢
ReplyDeleteThank you so much
ReplyDeleteOn ubuntu machine you can just install python-thrift package.
ReplyDelete