Mac下安装MySQLdb报错解决
Mac下安装MySQLdb报错解决
异类深呼吸 发表于4年前
Mac下安装MySQLdb报错解决
  • 发表于 4年前
  • 阅读 972
  • 收藏 0
  • 点赞 0
  • 评论 1

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: 看到这篇不错的文章直接转过来的,感谢这个哥们的分享

<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>

共有 人打赏支持
粉丝 5
博文 46
码字总数 42368
评论 (1)
zerobyte
could I install the module without the mysql server ?
×
异类深呼吸
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: