gpt4 book ai didi

python mysqldb 问题与 python 2.6 & 或 mysql 接口(interface)

转载 作者:行者123 更新时间:2023-11-30 23:39:53 24 4
gpt4 key购买 nike

我在 Python 2.6 和 Django 1.2.3 中安装了 MYSQL 和 MYSQLdb。我可以打开并登录到 mysql 没问题,但是当我检查它是否在 python 解释器中正常工作时,它没有与 python 接口(interface)。这是我在解释器中调用它时收到的错误消息....

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

最佳答案

看起来您正在 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

关于python mysqldb 问题与 python 2.6 & 或 mysql 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4320056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com