简体   繁体   English

在Mac上安装MySQL以与Python一起使用

[英]Installing MySQL on Mac for use with Python

I simply want to install MySQL on my Mac (running Mac OS X 10.6.5 (Snow Leopard)) for use with Python. 我只想在Mac(运行Mac OS X 10.6.5 (Snow Leopard))上安装MySQL以与Python一起使用。

So far I have: 到目前为止,我有:

  1. Downloaded and installed [mysql-5.5.8-osx10.6-x86_64.dmg]. 下载并安装[mysql-5.5.8-osx10.6-x86_64.dmg]。 (I have also accidentally downloaded and installed [mysql-5.1.54-osx10.6-x86_64.dmg]) (我也意外地下载并安装了[mysql-5.1.54-osx10.6-x86_64.dmg])

  2. Downloaded and installed [mySQL-python-1.2.3] 下载并安装[mySQL-python-1.2.3]

  3. Added the following to my .bash_profile: 在我的.bash_profile中添加了以下内容:

     [export PATH=$PATH:/usr/local/mysql/bin] 

But when I run: import mySQLdb in terminal, I am met with the following message: 但是,当我运行:在终端中import mySQLdb时,遇到以下消息:

Traceback (most recent call last): 追溯(最近一次通话):
File "<stdin>", line 1, in <module> <模块>中第1行的文件“ <stdin>”
ImportError: No module named mySQLdb ImportError:没有名为mySQLdb的模块

How can I fix this problem? 我该如何解决这个问题?

UPDATE: Okay, so I tried the MacPorts install, but still it is not working. 更新:好的,所以我尝试安装MacPorts ,但仍然无法正常工作。 I got the following error messages... 我收到以下错误消息...

Error: db46 requires the Java for Mac OS X development headers. 错误:db46需要Java for Mac OS X开发标头。
Error: Download the Java Developer Package from: https://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/getSoftware?bundleID=20719 错误:从以下网址下载Java开发人员软件包: https//connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/getSoftware?bundleID = 20719

Error: Target org.macports.configure returned: missing Java headers 错误:返回目标org.macports.configure:缺少Java标头

Error: Failed to install db46 错误:无法安装db46
Log for db46 is at: /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_release_ports_databases_db46/main.log db46的日志位于:/opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_release_ports_databases_db46/main.log

Error: The following dependencies were not installed: py26-distribute python26 db46 gdbm gettext expat libiconv gperf sqlite3 错误:未安装以下依赖项:py26-distribute python26 db46 gdbm gettext expat libiconv gperf sqlite3

Error: Status 1 encountered during processing. 错误:处理期间遇到状态1。

It doesn't mean much to me, though I'm guessing the second-to-last one explains why Python is not where it should be. 尽管我猜测倒数第二个解释了为什么Python不在应有的位置,但是这对我来说意义不大。

It is certainly possible to make it all work by yourself. 当然,可以自己完成所有工作。 But there are a lot of pieces and, especially on OS X 10.6 and its preference for running in 64-bit, it can be difficult to get everything right. 但是有很多方面,尤其是在OS X 10.6上以及它偏爱以64位运行的情况下,可能很难正确完成所有工作。 We could debug each step along the way; 我们可以调试过程中的每个步骤; to do that you are going to need to supply more information. 为此,您将需要提供更多信息。 Or you could do yourself a favor and install everything from a 3rd-party package manager, like MacPorts , Fink , or Homebrew . 或者,您可以帮自己一个忙,并从第三方软件包管理器安装所有组件,例如MacPortsFinkHomebrew That makes even more sense if you are going to be installing more packages. 如果您要安装更多软件包,那将更有意义。 I happen to prefer MacPorts . 我碰巧更喜欢MacPorts If you haven't already installed its base files, follow the instructions here . 如果尚未安装其基本文件,请按照此处的说明进行操作。 If you have installed it already, then do this to make sure the list of ports is up-to-date: 如果已经安装,请执行以下操作以确保端口列表是最新的:

$ sudo port selfupdate

Then you can install everything you need with one command: 然后,您可以使用一个命令安装所需的一切:

$ sudo port install py26-mysql

When it's done: 完成后:

$ /opt/local/bin/python2.6 -c "import MySQLdb; print(MySQLdb.version_info)"
(1, 2, 3, 'final', 0)

Update: based on the additional information you supplied, it appears that a recent change to the Java distribution on OS X 10.6 causes one of the dependent libraries, db46, to fail. 更新:根据您提供的附加信息,似乎OS X 10.6上Java发行版的最新更改导致相关库db46之一失败。 The easiest way around that should be to add a command to select its non-Java variant: 解决该问题的最简单方法是添加命令以选择其非Java变体:

$ sudo port clean db46
$ sudo port install db46 +no_java
$ sudo port install py26-mysql

This 这个

import MySQLdb

works for me (case is different). 为我工作(情况不同)。 Otherwise, make sure the egg is properly installed (eg in /Library/Python/2.6/site-packages/MySQL_python) 否则,请确保已正确安装鸡蛋(例如,在/Library/Python/2.6/site-packages/MySQL_python中)

I have had great luck using MAMP instead of installing MySQL and Python directly onto my Mac with Snow Leopard. 使用MAMP而不是直接使用Snow Leopard将MySQL和Python直接安装到我的Mac上,我很幸运。 I have heard horror stories of customized MySQL and Python installations breaking when you run certain updates from Apple. 当您从Apple运行某些更新时,我曾听到有关定制的MySQL和Python安装中断的恐怖故事。 That, and the ability to easily disable Apache, MySQL, PHP and Python from running in the background while not needed is why I went with MAMP. 那样,并且能够轻松禁用Apache,MySQL,PHP和Python在不需要时在后台运行,这就是我使用MAMP的原因。

Number 8 on this webpage walks you through how to get Python setup: http://www.sitepen.com/blog/2008/05/16/supercharge-mamp/ 此网页上的数字8引导您完成如何设置Python: http//www.sitepen.com/blog/2008/05/16/supercharge-mamp/

More information on MAMP can be found here: http://www.mamp.info/en/mamp/index.html 有关MAMP的更多信息,请参见: http : //www.mamp.info/en/mamp/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM