python第三方包--MySQLdb的安装

最近使用过一次MySQLdb,但是在安装时发生诸多错误,也参考过许多前辈的问题解决方案,再次深表谢意,不幸的是前辈们的链接我没记。。。。下面直接贴出来一个比较正确的做法,以供参考。

下载MySQL-1.2.5

点击此处下载MySQL-1.2.5
下载Download files/下的Source压缩包。将下载的压缩包MySQL-python-1.2-2.5解压,进入目录下,修改site.cfg文件:

    $ vim site.cfg


#mysql_config = /usr/local/bin/mysql_config
改为
mysql_config = /your_mysql_install_dir/bin/mysql_config

安装mysql

    $ brew install python

安装command line tools

    $ xcode-select --install

安装mysql-connector-c

安装mysql-connector-c时,系统会提醒此时已经链接到mysql,需要解除链接。

$ brew install mysql-connector-c
Error: Cannot install mysql-connector-c because conflicting formulae are installed.
mysql: because both install MySQL client libraries
Please `brew unlink mysql` before continuing.
Unlinking removes a formula's symlinks from /usr/local. You can
link the formula again after the install finishes. You can --force this install, but the build may fail or cause obscure side effects in the resulting software.

此时先解除链接,再安装mysql-connector-c,之后再恢复链接。

$ brew unlink mysql
$ brew install mysql-connector-c
$ brew link --overwrite mysql

安装MySQL-python

$ pip install MYSQL-python

问题记录

安装MYSQL-python结束之后尝试运行代码,发现报错:

ImportError: this is MySQLdb version (1, 2, 5, 'final', 1), but _mysql is version (1, 4, 2, 'post', 1)

根据运行栈查找发现,_mysqlMYSQLdbversion都是1.2.5。再查找资料发现,此处的1.4.2post1是第三方库mysqlclient的版本号,因为之前曾经安装过mysqlclient而导致程序运行时所获取的版本号出错。于是卸载mysqlclient并重新安装:

$ pip uninstall mysqlclient
$ pip install mysqlclient

再尝试发现运行正确了。

猜你喜欢

转载自blog.csdn.net/weixin_44809746/article/details/90044909