Python中最链接Mysql经常使用的驱动是html
mysql-python :mysql的C语言的驱动 mysql-connector:msql官方的驱动 pymysql:python语言的驱动
我这里安装的是 mysql-pythonpython
终端中执行mysql
pip install mysql-python
运气好的的话,直接就成功了,我遇到了一下问题,最终找到了解决方案,缘由及解决方案以下,供参考linux
终端打印出:sql
Collecting mysql-python Downloading MySQL-python-1.2.5.zip (108kB) 100% |████████████████████████████████| 110kB 30kB/s Complete output from command python setup.py egg_info: sh: mysql_config: command not found Traceback (most recent call last): File "<string>", line 20, in <module> File "/private/tmp/pip-build-NP8J3v/mysql-python/ setup.py", line 17, in <module> metadata, options = get_config() File "setup_posix.py", line 43, in get_config libs = mysql_config("libs_r") File "setup_posix.py", line 25, in mysql_config raise EnvironmentError("%s not found" % (mysql_config.path,)) EnvironmentError: mysql_config not found ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /private/tmp/pip-build-NP8J3v/mysql-python
解决方法,执行:vim
ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config
缘由
找不到mysql_config通常是因为经过lnmp.org或者其余方式安装mysql之后mysql_config是在/usr/local/mysql/bin/里面,这里面的文件不是在任意位置均可以访问的,而指令是segmentfault
将mysql_config连接到/usr/local/bin目录下api
参考一条命令解决mysql_config not foundbash
出错信息为:python2.7
Traceback (most recent call last): File "manage.py", line 4, in <module> from models import User File "/Users/tengfei/PycharmProjects/mysql/models.py", line 1, in <module> import MySQLdb File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module> import _mysql ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Library/Python/2.7/site-packages/_mysql.so Reason: image not found
解决:
pip install --upgrade pip
修改 OS X 环境变量:打开终端,在终端中使用 vim 打开 “~/.bash_profile”,若是没有安装 vim,那就显示隐藏文件用文本编辑器打开,具体操做这里就不复述了。在 .bash_profile 中添加如下内容
PATH="/usr/local/mysql/bin:${PATH}" export PATH export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/ export VERSIONER_PYTHON_PREFER_64_BIT=no export VERSIONER_PYTHON_PREFER_32_BIT=yes
其中 VERSIONER_PYTHON_PREFER_64_BIT 和 VERSIONER_PYTHON_PREFER_64_BIT 根据本身安装的 MySQL 进行选择。
参考:
MySQLdb(即 MySQL-python 包)在 OS X 中安装指南
Solving Library not loaded: libmysqlclient.18.dylib when importing MySQLdb on Lion
Mac OS + MySQL-python-1.2.3: ImportError | Library not loaded | libmysqlclient.16.dylib
执行pip install mysql-python后的出错信息以下:
这是在虚拟环境中见到的
^
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
---------------------------------------- Command "/home/tengfei/api01/test/TestTo/TODO-orm/venv/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-w6mp1W/mysql-python/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-jG7EKf-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/tengfei/api01/test/TestTo/TODO-orm/venv/include/site/python2.7/mysql-python" failed with error code 1 in /tmp/pip-build-w6mp1W/mysql-python/
缘由是:一些依赖包没有安装,注意这也是不少实用pip执行安装扩展的时候,常见的错误
apt-get build-dep python-lxml sudo pip install lxml --upgrade pip install mysql-python