0

可能重复:
如何在 Mac OS X 上安装 MySQLdb(MySQL 的 Python 数据访问库)?

尽管此问题之前已在此处提出,但此处发布的问题和后续答案与 OS X (Snow Leopard) 相关,与 OS X Lion 和 OS X Mountain Lion 无关。OS X Lion 和 OS X Mountain Lion 中提供了 Python 和 MySQL 补丁,这些补丁会影响在 Mac OS X 10.7 (Lion) 和 Mac OS X 10.8 (Mountain Lion) 上顺利安装 Python-MySQLdb-connector 的过程。这是重复这个问题的唯一原因。如果您运行的是旧版本的 OS X,请随时参考旧问题。

我正在运行 OS X 10.8.2 (Mountain Lion) 并安装了 python 2.7.2 -

==> python 
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

但是当我在 python 上运行 import MySQLdb 时,它会失败并显示以下消息 -

==> python 
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb

任何输入都会有所帮助。

4

2 回答 2

6

我按照以下步骤解决了上述问题:

以下步骤适用于 Mountain Lion (OS X 10.8.2) -

安装 MySQL-python 包

在此处下载最新的 MySQL(64 位,tar 存档)

  • 提取焦油含量
  • 将文件夹重命名为“mysql”</li>
  • 将文件夹复制到 /usr/local/
  • 您现在应该在 /usr/local/mysql 上安装了 mysql

在此处下载最新的 MySQL-python 包

在终端上输入:

tar -xvzf MySQL-python-1.2.3.tar.gz cd MySQL-python-1.2.3

在您喜欢的文本编辑器上编辑 site.cfg 并更改 mysql 路径如下: mysql_config = /usr/local/mysql/bin/mysql_config

您现在已准备好构建和安装 MySQL-python:

sudo python setup.py build sudo python setup.py install

Edit your ~/.bash_profile, add the following line:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

If you are already using MAMP you still have to follow the above MySQL installation process, as MAMP by default doesn’t include the required header files to build MySQL-python. Moreover if you prefer MAMP, you don’t have to run mysql from /usr/local/mysql. On your settings.py remember to set “127.0.0.1″ as your preferred db host in case that “localhost” doesn’t work.

Test MySQL-python installation

Restart Terminal and type:

python import MySQLdb

The other thing I noticed is that it helps to restart the mac after installing mysql.

The steps posted on the following link work for OS X 10.6.8 -

How to install MySQLdb (Python data access library to MySQL) on Mac OS X?

于 2013-01-26T06:38:32.880 回答
0

mysql-connector-python from offical mysql(Oracle) is a good choice for both python 2.x and 3.x.

It's a pure python library without mysql client.

于 2013-01-26T07:05:55.477 回答