我有MYSQL& MYSQLdb安装了Python 2.6& Django 1.2.3。我可以打开&登录到mysql没有问题但是当我检查它是否在python解释器中正常工作时它没有与python接口。这是我在解释器中调用它时遇到的错误信息....
import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/_mysql.so, 2): Library not loaded: /opt/local/lib/mysql5/mysql/libmysqlclient_r.15.dylib
Referenced from: /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/_mysql.so
Reason: image not found
答案 0 :(得分:0)
Python无法找到'libmysqlclient_r.15.dylib'文件。您可能有不匹配的MySQL和MYSQLdb版本,或者该文件不在标准位置。尝试重新安装MYSQLdb。
答案 1 :(得分:0)
看起来您在MacOS X上使用MacPorts
Python安装。如果正确使用MacPorts
进行升级,则不应遇到类似问题。首先,确保您的安装是最新的:
$ sudo port selfupdate
$ sudo port outdated # lists outdated ports
$ sudo port -u upgrade outdated # upgrade outdated ports and remove old versions
然后,如果您仍有问题,请确保您在某处没有文件权限问题。看起来应该是这样的:
$ ls -l /opt/local/lib/mysql5/mysql
total 93824
-rw-r--r-- 2 root admin 1824 Nov 22 16:10 libdbug.a
-rw-r--r-- 2 root admin 161176 Nov 22 16:10 libheap.a
-rw-r--r-- 2 root admin 900552 Nov 22 16:10 libmyisam.a
-rw-r--r-- 2 root admin 136472 Nov 22 16:10 libmyisammrg.a
-rwxr-xr-x 2 root admin 2871400 Nov 22 16:10 libmysqlclient.16.dylib*
-rw-r--r-- 2 root admin 3224032 Nov 22 16:10 libmysqlclient.a
lrwxr-xr-x 1 root admin 23 Nov 22 16:10 libmysqlclient.dylib@ -> libmysqlclient.16.dylib
-rwxr-xr-x 2 root admin 1028 Nov 22 16:10 libmysqlclient.la*
-rwxr-xr-x 2 root admin 2891208 Nov 22 16:10 libmysqlclient_r.16.dylib*
-rw-r--r-- 2 root admin 3248504 Nov 22 16:10 libmysqlclient_r.a
lrwxr-xr-x 1 root admin 25 Nov 22 16:10 libmysqlclient_r.dylib@ -> libmysqlclient_r.16.dylib
-rwxr-xr-x 2 root admin 1040 Nov 22 16:10 libmysqlclient_r.la*
-rw-r--r-- 2 root admin 25939344 Nov 22 16:10 libmysqld.a
-rw-r--r-- 2 root admin 2643984 Nov 22 16:10 libmystrings.a
-rw-r--r-- 2 root admin 667720 Nov 22 16:10 libmysys.a
-rwxr-xr-x 2 root admin 2039856 Nov 22 16:10 libndbclient.3.dylib*
-rw-r--r-- 2 root admin 3221192 Nov 22 16:10 libndbclient.a
lrwxr-xr-x 1 root admin 20 Nov 22 16:10 libndbclient.dylib@ -> libndbclient.3.dylib
-rwxr-xr-x 2 root admin 976 Nov 22 16:10 libndbclient.la*
-rw-r--r-- 2 root admin 29568 Nov 22 16:10 libvio.a
drwxr-xr-x 2 root admin 612 Nov 22 16:10 plugin/
还要确保没有架构不匹配:
$ cd /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
$ file _mysql.so
_mysql.so: Mach-O universal binary with 2 architectures
_mysql.so (for architecture x86_64): Mach-O 64-bit bundle x86_64
_mysql.so (for architecture i386): Mach-O bundle i386
$ otool -L _mysql.so # this shows which MySQL client lib the Python DB adapter is dynamically linked to
_mysql.so:
/opt/local/lib/mysql5/mysql/libmysqlclient_r.16.dylib (compatibility version 17.0.0, current version 17.0.0)
/opt/local/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)
/opt/local/lib/libssl.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
/opt/local/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
$ cd /opt/local/lib/mysql5/mysql/
$ file libmysqlclient_r.16.dylib
libmysqlclient_r.16.dylib: Mach-O universal binary with 2 architectures
libmysqlclient_r.16.dylib (for architecture i386): Mach-O dynamically linked shared library i386
libmysqlclient_r.16.dylib (for architecture x86_64): Mach-O 64-bit dynamically linked shared library x86_64
当然,请确保您确实使用的是MacPorts
Python:
$ /opt/local/bin/python2.6 -V
Python 2.6.6
$ /opt/local/bin/python2.6 -c 'import MySQLdb; print(MySQLdb._mysql.__file__)'
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated
from sets import ImmutableSet
/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/_mysql.so