Mac下安装MySQL-python-1.2.5问题
在Mac下,使用Python2.7操作MySQL需要导入MySQLdb,在代码中执行以下代码
import MySQLdb
- 1
如果执行后的输出结果如下所示,意味着你没有安装MySQLdb模块:
Traceback (most recent call last):
File "./hello.py", line 3, in <module>
import MySQLdb
ImportError: No module named MySQLdb
- 1
- 2
- 3
- 4
此时你可以到 https://pypi.org/project/MySQL-python/
下载对应的版本,这里我也提供了百度云盘下载链接 MySQL-python-1.2.5版本
解压下载的文件,然后切换到相应的目录,执行安装命令:
$ cd MySQL-python-1.2.5
$ python setup.py build
$ python setup.py install
- 1
- 2
- 3
如果提示以下信息则表示安装成功:
Processing dependencies for MySQL-python==1.2.5
Finished processing dependencies for MySQL-python==1.2.5
- 1
- 2
然而,安装过程并不总是一帆风顺,在此,我记录下自己安装过程中遇到的问题及解决办法。
####错误1:EnvironmentError: mysql_config not found
####
- 在shell中执行
sudo find / -name mysql_config
命令
$ sudo find / -name mysql_config
Password:
find: /dev/fd/MySQL-python-1.2.4b4: No such file or directory
find: /dev/fd/MySQL-python-1.2.4b4: No such file or directory
/usr/local/bin/mysql_config
/usr/local/Cellar/mysql-connector-c/6.1.11/bin/mysql_config
/usr/local/mysql-8.0.11-macos10.13-x86_64/bin/mysql_config
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 修改
setup_posix.py
中
mysql_config.path = "mysql_config"
- 1
修改为我本机的`mysql_config`配置路径:
- 1
mysql_config.path = "/usr/local/mysql-8.0.11-macos10.13-x86_64/bin/mysql_config"
- 1
以下错误我也是通过修改mysql_config
配置来解决的:
Traceback (most recent call last):
File "setup.py", line 18, in <module>
metadata, options = get_config()
File "/Users/wang/Downloads/MySQL-python-1.2.4b4/setup_posix.py", line 56, in get_config
libraries = [ dequote(i[2:]) for i in libs if i.startswith(compiler_flag("l")) ]
File "/Users/wang/Downloads/MySQL-python-1.2.4b4/setup_posix.py", line 11, in dequote
if s[0] in "\"'" and s[0] == s[-1]:
IndexError: string index out of range
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
####错误2:error: command 'cc' failed with exit status 1
####
通过brew命令安装 libxml2 和 libxslt 这两个库
brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force
- 1
- 2
- 3
- 4
错误3:error: command 'clang' failed with exit status 1
####
Mac的OSX系统的C语言,编译器用的是Clang。既然报错与clang有关,应该是xcode command tools出现问题,执行 xcode-select --install
重新安装。
错误4:_mysql.c:44:10: fatal error: 'my_config.h' file not found
这个问题应该是可以通过配置mysql_config的路径解决的,但是如果是找不到,可以单独安装mysq-connector-c
Mac下执行以下命令:
brew install mysql-connector-c