天天看點

已經裝了MySQL-python,但在引用時報錯:ImportError: No module named MySQLdb

導入MySQLdb子產品的時候報錯,如下

>>> import MySQLdb

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

ImportError: No module named MySQLdb

在網上找了一個解決方法:

yum install MySQL-python  (centos系統)

再次導入的時候還是報錯,懷疑是版本問題:

$ python --version

Python 2.7.10

$ cat /etc/issue

CentOS release 5.11 (Final)

Kernel \r on an \m

這個版本的系統預設裝的是python2.4的子產品:

$ rpm -ql MySQL-python

/usr/lib64/python2.4/site-packages/MySQL_python-1.2.3c1-py2.4.egg-info

/usr/lib64/python2.4/site-packages/MySQL_python-1.2.3c1-py2.4.egg-info/PKG-INFO

/usr/lib64/python2.4/site-packages/MySQL_python-1.2.3c1-py2.4.egg-info/SOURCES.txt

/usr/lib64/python2.4/site-packages/MySQL_python-1.2.3c1-py2.4.egg-info/dependency_links.txt

/usr/lib64/python2.4/site-packages/MySQL_python-1.2.3c1-py2.4.egg-info/top_level.txt

/usr/lib64/python2.4/site-packages/MySQLdb

........

解決方法:

2,unzip  MySQL-python-1.2.5.zip

3,安裝

cd MySQL-python-1.2.5 

python setup.py build

python setup.py install

再次裝載子產品的時候還是報錯:

  File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>

  File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in <module>

  File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__

ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory

根據最後提示,應該是找不着一個交libmysqlclient.so.18的檔案,于是到mysql安裝目錄裡找到這個檔案并且做一個軟連接配接到/usr/lib

ln -sv /usr/local/ver01/percona/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18

再次裝載子產品的時候不再報錯了。

本文轉自 emma_cql 51CTO部落格,原文連結:http://blog.51cto.com/chenql/1760899