文档章节

Mac下安装MySQLdb报错解决

异类深呼吸
 异类深呼吸
发布于 2014/07/11 18:43
字数 891
阅读 975
收藏 0

<p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">还是老样子,本来想自己写的,但是发现这个哥们写的相当详细,直接搬过来即可</p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  安装环境:OS X操作系统,Python 2.7.3。</p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  MySQLdb其实包含在MySQL-python包中,因此无论下载还是在pip中search,都应该是搜寻MySQL-python。</p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  以下将说明MySQLdb两种常见的安装方式,下载安装或者pip安装MySQL-python。<span style="margin: 0px; padding: 0px; font-size: 18pt;"><strong style="margin: 0px; padding: 0px;"> </strong></span></p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;"><span style="margin: 0px; padding: 0px; font-size: 18pt;"><strong style="margin: 0px; padding: 0px;"> 下载MySQLdb</strong></span></p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  在SourceForge可以下载<a style="margin: 0px; padding: 0px; color: #0069d6; line-height: inherit;" href="http://sourceforge.net/projects/mysql-python/">MySQL-python-1.2.4b4.tar</a>,下载后解压,然后在终端Terminal中执行以下命令:</p> <div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid #cccccc; overflow: auto; background-color: #f5f5f5; font-family: 'Courier New' !important; font-size: 12px !important;"> <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">$ cd /Users/iFantastic/Downloads/MySQL-python-<span style="margin: 0px; padding: 0px; color: #800080; line-height: 1.5 !important;">1.2</span>.4b4<br style="margin: 0px; padding: 0px;" />$ python setup.py install</pre> </div> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;"><strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-size: 18pt;"> 使用pip安装MySQLdb</span></strong></p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  在终端中执行:</p> <div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid #cccccc; overflow: auto; background-color: #f5f5f5; font-family: 'Courier New' !important; font-size: 12px !important;"> <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">$ pip <span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important;">install</span> MySQL-python</pre> </div> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  无论是在线安装还是下载安装,此时你可能会遇到第一个错误提示:</p> <div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid #cccccc; overflow: auto; background-color: #f5f5f5; font-family: 'Courier New' !important; font-size: 12px !important;"> <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">EnvironmentError: mysql_config <span style="margin: 0px; padding: 0px; color: #0000ff; line-height: 1.5 !important;">not</span> found</pre> </div> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  建议使用pip安装,因为我从sourceforge下载的版本居然不如pip中的新。如果想要升级mysql-python版本,可以使用以下命令:</p> <div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid #cccccc; overflow: auto; background-color: #f5f5f5; font-family: 'Courier New' !important; font-size: 12px !important;"> <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">$ easy_install MySQL-python --upgrade</pre> </div> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  [注意]如果你没有按照下文解决安装中的错误提示,那么升级也会因为同样的错误原因而失败。</p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;"><strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-size: 18pt;"> 解决mysql_config not found错误</span></strong></p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  在默认方式安装Mac版MySql时,会在/usr/local/mysql/bin目录下生成mysql_config文件。</p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  因此<span style="margin: 0px; padding: 0px; font-size: 18px;"><strong style="margin: 0px; padding: 0px;">下载安装</strong></span>时的解决办法为:在MySQL-python的安装包中找到site.cfg文件,打开它,找到以下内容:</p> <div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid #cccccc; overflow: auto; background-color: #f5f5f5; font-family: 'Courier New' !important; font-size: 12px !important;"> <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;"><span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important;">#</span><span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important;"> The path to mysql_config.</span><span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important;"> <br />#</span><span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important;"> Only use this if mysql_config is not on your PATH, or you have some weird</span><span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important;"> <br />#</span><span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important;"> setup that requires it.</span><span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important;"> <br />#</span><span style="margin: 0px; padding: 0px; color: #008000; line-height: 1.5 !important;"> mysql_config = /usr/local/bin/mysql_config</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"><br style="margin: 0px; padding: 0px;" /></span></pre> </div> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  将最后一句句首井号去掉,并修改为:</p> <div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid #cccccc; overflow: auto; background-color: #f5f5f5; font-family: 'Courier New' !important; font-size: 12px !important;"> <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">mysql_config = /usr/local/mysql/bin/mysql_config</pre> </div> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  然后执行:</p> <div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid #cccccc; overflow: auto; background-color: #f5f5f5; font-family: 'Courier New' !important; font-size: 12px !important;"> <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">$ python setup.py install</pre> </div> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  一般说来,此时安装可以完成,但仍有问题,下文会继续阐述。</p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  使用<span style="margin: 0px; padding: 0px; font-size: 18px;"><strong style="margin: 0px; padding: 0px;">pip安装</strong></span>时没有办法修改site.cfg文件,因此可以通过修改OS X的系统环境变量来解决找不到mysql_config的错误。</p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  <strong style="margin: 0px; padding: 0px;">修改<span style="margin: 0px; padding: 0px; font-size: 18px;">OS X环境变量</span></strong>:打开终端,在终端中使用vim打开“<span style="margin: 0px; padding: 0px;">~/.bash_profile”,如果没有安装vim,那就显示隐藏文件用文本编辑器打开,具体操作这里就不复述了。在.bash_profile中添加以下内容:</span></p> <div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid #cccccc; overflow: auto; background-color: #f5f5f5; font-family: 'Courier New' !important; font-size: 12px !important;"> <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">PATH=<span style="margin: 0px; padding: 0px; color: #800000; line-height: 1.5 !important;">"</span><span style="margin: 0px; padding: 0px; color: #800000; line-height: 1.5 !important;">/usr/local/mysql/bin:${PATH}</span><span style="margin: 0px; padding: 0px; color: #800000; line-height: 1.5 !important;">"</span><span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> <br />export PATH <br />export DYLD_LIBRARY_PATH</span>=/usr/local/mysql/lib/<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;"> <br />export VERSIONER_PYTHON_PREFER_64_BIT</span>=<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">no <br />export VERSIONER_PYTHON_PREFER_32_BIT</span>=yes</pre> </div> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  其中 VERSIONER_PYTHON_PREFER_64_BIT和VERSIONER_PYTHON_PREFER_64_BIT根据自己安装的MySQL进行选择。</p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  另外再提供一个pip安装时找不到mysql_config的解决方法(我没有验证),在终端中输入以下命令:</p> <div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid #cccccc; overflow: auto; background-color: #f5f5f5; font-family: 'Courier New' !important; font-size: 12px !important;"> <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">$ sudo ln -s /usr/local/mysql/bin/* /usr/bin</pre> </div> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  好了,到这里,MySQL-python包应该基本顺利安装。</p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;"><strong style="margin: 0px; padding: 0px;"><span style="margin: 0px; padding: 0px; font-size: 18pt;"> 解决 Reason: image not found 错误</span></strong></p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;"><span style="margin: 0px; padding: 0px;">  安装完MySQL-python包后,让我们import MySQLdb,此时出现一个错误,错误最后一行写着 Reason: image not found。</span></p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;"><span style="margin: 0px; padding: 0px;">  解决方法是在终端执行:</span></p> <div class="cnblogs_code" style="margin: 5px 0px; padding: 5px; border: 1px solid #cccccc; overflow: auto; background-color: #f5f5f5; font-family: 'Courier New' !important; font-size: 12px !important;"> <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">$ sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18<span style="margin: 0px; padding: 0px; line-height: 1.5 !important;">.dylib <br />$ sudo ln </span>-s /usr/local/mysql/lib /usr/local/mysql/lib/mysql</pre> </div> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">  之后重启idle,成功导入MySQLdb。</p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;"> </p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">PS: 在Ubuntu下使用pip安装MySQL-python同样会出现mysql_config no found的错误提示,建议使用 sudo apt-get install python-mysqldb 来进行安装,这样直接可以安装成功。</p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;"> </p> <p style="font-size: 13px; margin: 10px auto; padding: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 19.5px; background-color: #f5f5f5;">如果使用MacPorts安装MySQL,MacPorts安装的源码包都存放在/opt目录下,安装完MySQL后,使用 port select --list mysql 查看一下当前启用的MySQL版本(初次安装完毕后应该是没有启用任何版本的MySQL),然后使用 port select --set mysql mysql_you_selected 来设置启用的MySQL版本,之后再使用pip安装则无需担心mysql-config的问题。</p>

本文转载自:http://www.cnblogs.com/ifantastic/archive/2013/04/13/3017677.html

共有 人打赏支持
异类深呼吸
粉丝 5
博文 46
码字总数 42368
作品 0
朝阳
系统管理员
加载中

评论(1)

zerobyte
zerobyte
could I install the module without the mysql server ?
Mac os 安装 MySQL和MySQL-Python

Mac是我的工作机,需要做python Web 开发,但是坑爹的是MySQLdb 一直无法正常工作,一气之下我选择重新安装。 首先是重新安装系统,参考下个链接的文章,并且建议做好time machine备份系统。...

xxjbs001
2015/12/27
85
0
Mysql-Python 在 Mac osx下的安装

环境配置这个东西很讨厌,但是还必须得配,没办法。这个环境的配置过程也是很多坑,花了一个下午踩坑,由于重启了几次,所以没有截图来记录了。不过还是可以分享一下遇到问题每一步的分析和解...

doctordc
05/14
0
0
Centos下安装MySQLdb若干错误总结

1、下载,解压python-mysql 2、运行python setup.py install 3、Error1: 报错mysql_config not found 首先查找mysqlconfig的位置,使用find / -name mysqlconfig 其次、修改setup_posix.py文......

shaohan
2014/03/05
0
0
Python 安装mysql (MAC)

一、mysqldb模块 http://sourceforge.net/projects/mysql-python/ 二、解压安装 解压 vi setup_posix.py mysqlconfig.path = "/usr/local/mysql/bin/mysqlconfig" sudo python setup.py ins......

HarryWu
2015/10/20
250
0
python mysqldb 遇到的问题

安装django用到mysqldb pip install MySQL-python 报错 EnvironmentError: mysql_config not found 解决办法 export PATH="/usr/local/mysql/bin:$PATH" 然后继续安装 sudo pip install MyS......

zjl_longer
2013/12/27
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

[雪峰磁针石博客]python3快速入门教程1 turtle绘图-2函数

菲波那契序列: >>> # Fibonacci series:... # the sum of two elements defines the next... a, b = 0, 1>>> while b < 10:... print(b)... a, b = b, a+b...112......

python测试开发人工智能安全
今天
0
0
java环境变量配置最正确的方式

原贴:https://blog.csdn.net/qq_40007997/article/details/79784711,十分详细,亲测有效

kitty1116
今天
0
0
49.Nginx防盗链 访问控制 解析php相关 代理服务器

12.13 Nginx防盗链 12.14 Nginx访问控制 12.15 Nginx解析php相关配置(502的问题) 12.16 Nginx代理 扩展 502问题汇总 http://ask.apelearn.com/question/9109 location优先级 http://blog....

王鑫linux
今天
1
0
Nginx防盗链、访问控制、解析php相关配置、Nginx代理

一、Nginx防盗链 1. 编辑虚拟主机配置文件 vim /usr/local/nginx/conf/vhost/test.com.conf 2. 在配置文件中添加如下的内容 { expires 7d; valid_referers none blocked server_names *.tes......

芬野de博客
今天
0
0
spring EL 和资源调用

资源调用 import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.PropertySource;import org.springframework.core.io.Resource;......

Canaan_
今天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部