Centos下python安装 MySQLdb报错

python2.7在 调用MySQLdb时报以下错误:

ImportError: /home/op1/.python-eggs/MySQL_python-1.2.3-py2.7-linux-x86_64.egg-tmp/_mysql.so: undefined symbol: __cxa_pure_virtual

问题分析与解决办法:

经查得知是由于编译Python所用的编译器与编译MySQL-python所用编译器不同所致。
用g++重新编译_mysql.so
#cd MySQL-python-1.2.3/build
#g++ -pthread -shared ./temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64 -l mysqlclient_r -lpthread -lm -lrt -ldl -o ./lib.linux-x86_64-2.7/_mysql.so

之后重新安装MySQLdb,import MySQLdb是错误消除,问题解决。不过有两个警告(不影响MySQLdb模块的正常使用):
UserWarning: /home/op1/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.pyc, but /home/op1/MySQL-python-1.2.3 is being added to sys.path

第一个警告可通过修改/home/op1/.python-eggs目录的权限解决
sudo chmod g-w /home/op1/.python-eggs
sudo chmod o-w /home/op1/.python-eggs
第二个警告是指安装后MySQLdb模块已经被放到python的site-pachages目录中,但在当前目录也存在相同的模块,所以可能会重复导入。只要切换到其他目录运行就可以。

你可能感兴趣的:(Python)