在 Mac OS X 上安装 MySQL Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16182294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:58:55  来源:igfitidea点击:

Installing MySQL Python on Mac OS X

pythonmysqlmacos

提问by Peter Foti

Long story short, when I write the following:

长话短说,当我写下以下内容时:

sudo easy_install MySQL-python

I get the error

我收到错误

EnvironmentError: mysql_config not found

环境错误:找不到 mysql_config

All right, so there are plenty of threads and the like on how to fix that, so I run this code:

好的,所以有很多关于如何解决这个问题的线程等等,所以我运行了这个代码:

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

Then I rerun my sudo code:

然后我重新运行我的 sudo 代码:

sudo easy_install MySQL-python

Then I get the following error.

然后我收到以下错误。

Setup script exited with error: command 'llvm-gcc-4.2' failed with exit status 1

安装脚本退出并出现错误:命令“llvm-gcc-4.2”失败,退出状态为 1

Google/Stack Overflow that, and I am told to download a GCCpackage which I did the other day, 200 MB's or there-abouts and still no fix.

谷歌/堆栈溢出,我被告知要下载我前几天做的GCC包,200 MB 或大约,但仍然没有修复。

At this point I am lost, they say insanity is doing the same thing over and over while expecting a different result. Well, I've continually run the aforementioned code expecting a different result, so I'm not to far away from going insane.

在这一点上我迷失了,他们说疯狂是一遍又一遍地做同样的事情,同时期待不同的结果。好吧,我一直在运行上述代码,期望得到不同的结果,所以我离疯狂不远了。

At this point in my Python career, I am new to this, but I am willing to try pretty much anything to get this up and running.

在我的 Python 职业生涯的这个阶段,我是新手,但我愿意尝试几乎任何事情来启动和运行它。

If it helps I am officially running, Mac OS X 10.7.5, and I do have MAMPinstalled (is that an issue?)

如果它有助于我正式运行 Mac OS X 10.7.5,并且我确实安装了MAMP(这是一个问题吗?)

Also, the other day when I was trying all of this for the first time I installed (reinstalled?) MySQL, so I'm really in a tough spot at this point.

此外,前几天,当我第一次尝试安装(重新安装?)MySQL 时,我正在尝试所有这些,所以此时我真的处于困境。

Is there a fix?

有解决办法吗?

I've racked my brain, searched Google, read Stack Overflow, and spent hours trying to figure this out to no avail.

我绞尽脑汁,搜索了谷歌,阅读了 Stack Overflow,并花了几个小时试图解决这个问题,但无济于事。

采纳答案by Paul Jtheitroademan

Another option is to use pymysqlit is a pure Python client connection to MySQL so you don't have to mess around with compiling, a good exercise, but it can be frustrating if you are just trying to get something done. pymysql follows the same API as MySQLdb, it can essentially be used as a drop in replacement.

另一种选择是使用pymysql,它是到 MySQL 的纯 Python 客户端连接,因此您不必为编译而烦恼,这是一个很好的练习,但如果您只是想完成某事,则可能会令人沮丧。pymysql 遵循与 MySQLdb 相同的 API,它本质上可以用作替代品。

Also, it used to be that MySQLdb, did not work with Python 3, but this may have changed, pymysql didn't have that problem which also induced me to switch, this may have changed though. pymysql can be slower than MySQLdb but you'll have to see if you notice that, it is also under a different license (MIT for pymysql, GPL for MySQLdb)

另外,曾经是 MySQLdb 不能与 Python 3 一起使用,但这可能已经改变了,pymysql 没有那个问题也促使我切换,尽管这可能已经改变了。pymysql 可能比 MySQLdb 慢,但您必须查看是否注意到这一点,它也使用不同的许可证(pymysql 为 MIT,MySQLdb 为 GPL)

回答by Chan

The issue you are having is that the gcccompiler is not installed on your Mac. It will be installed if you have installed XCode. You will have to download gcccomplier and install it manually. Follow the below link and download it -

您遇到的问题是gcc您的 Mac 上未安装编译器。如果您已经安装了 XCode,它将被安装。您必须下载gcc编译器并手动安装。请按照以下链接下载 -

https://github.com/downloads/kennethreitz/osx-gcc-installer/GCC-10.7-v2.pkg

https://github.com/downloads/kennethreitz/osx-gcc-installer/GCC-10.7-v2.pkg

I once had this problem installing Ruby 1.9 and I had to compile ruby for myself because Mountain Lion wasn't supported at that time. After installing the package, verify the install by the command gcc.

我曾经在安装 Ruby 1.9 时遇到过这个问题,我不得不为自己编译 ruby​​,因为当时不支持 Mountain Lion。安装包后,通过命令验证安装gcc

回答by 7stud

It's time to be a big boy and install from source. Try this:

是时候成为一个大男孩并从源代码安装了。尝试这个:

1) Download the MySQL-python-1.X.X.tar.gz file(by default will go to your Downloads directory)

1) 下载 MySQL-python-1.XXtar.gz 文件(默认会进入你的下载目录)

2) Open a Terminal window and cd to the Downloads directory.

2) 打开终端窗口并 cd 到下载目录。

3) Unzip the file you downloaded:

3) 解压您下载的文件:

~/Downloads$ tar xfvz MySQL-python-1.X.X.tar.gz

That will create a directory inside your Downloads directory called MySQL-python

这将在您的下载目录中创建一个名为 MySQL-python 的目录

4) cd into the newly created directory.

4) cd 进入新创建的目录。

5) Typically, you just open the file called README or INSTALL and follow the instructions--but generally to install a python module all you do is:

5) 通常,您只需打开名为 README 或 INSTALL 的文件并按照说明进行操作——但通常要安装 python 模块,您只需:

$ sudo python setup.py install

If you care to look, there should be a file called setup.py inside your newly created MySQL-python directory, and you are invoking that program to install the module.

如果您愿意查看,在您新创建的 MySQL-python 目录中应该有一个名为 setup.py 的文件,并且您正在调用该程序来安装该模块。

Also note that this:

还要注意的是:

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

is not permanent if you did that on the command line. You need to put that line in a file called .bashrc in your home directory (~/ or equivalently /Users/YOUR_USER_NAME). To see if .bashrc already exists(it's a hidden file), issue the command:

如果您在命令行上执行此操作,则不是永久性的。您需要将该行放在主目录(~/ 或等效的 /Users/YOUR_USER_NAME)中名为 .bashrc 的文件中。要查看 .bashrc 是否已经存在(它是一个隐藏文件),请发出以下命令:

$ ls -al 

and look for .bashrc. If .bashrc doesn't exist, then create it.

并寻找 .bashrc。如果 .bashrc 不存在,则创建它。

回答by Toren

As others mentioned before me....getting Python to work with MySQL on a Mac is a ?@#$@&%^!! nightmare.

正如我之前提到的其他人......让 Python 在 Mac 上与 MySQL 一起工作是一个 ?@#$@&%^!! 恶梦。

Installed Django framework on Mac OS 10.7.5 initially from the original Django website and when the MySQLdb didn't work, and after many hours googling and trying solutions from SO, I have installed the Django stack from BitNami http://bitnami.com/stack/django

最初从原始 Django 网站在 Mac OS 10.7.5 上安装了 Django 框架,当 MySQLdb 无法正常工作时,经过数小时的谷歌搜索并尝试了 SO 的解决方案,我已经从 BitNami http://bitnami.com安装了 Django 堆栈/堆栈/Django

Still, got the issues mentioned above and then some more...

尽管如此,还是遇到了上面提到的问题,然后还有更多……

What helped me eventually is what Josh recommends on his blog: http://joshbranchaud.com/blog/2013/02/10/Errors-While-Setting-Up-Django.html

最终帮助我的是 Josh 在他的博客上推荐的内容:http: //joshbranchaud.com/blog/2013/02/10/Errors-While-Setting-Up-Django.html

Now Python 2.7 is finally connected to MySQL 5.5

现在 Python 2.7 终于连接到 MySQL 5.5

回答by Jude

Install mysql via homebrew, then you can install mysql python via pip.

通过homebrew安装 mysql ,然后就可以通过 pip 安装 mysql python。

pip install MySQL-python

It works for me.

这个对我有用。

回答by m01

Here's what I would install, especially if you want to use homebrew:

这是我要安装的,特别是如果你想使用自制软件

  • XCode and the command line tools (as suggested by @7stud, @kjti)
  • Install homebrew
  • brew install mysql-connector-c
  • pip install mysql-python
  • XCode 和命令行工具(如@7stud、@kjti 所建议的)
  • 安装自制软件
  • brew install mysql-connector-c
  • pip install mysql-python

回答by jasmo2

I am using OSX -v 10.10.4. The solution above is a quick & easy.

我正在使用 OSX -v 10.10.4。上面的解决方案是一种快速简便的.

Happening OSX does not have the connection library by default.

默认情况下,Happening OSX 没有连接库。

First you should install the connector:

首先,您应该安装连接器:

brew install mysql-connector-c

Then install with pip mysql

然后用pip mysql安装

pip install mysql-python

回答by HelloObamama

I am using Python 2.7.11 :: Anaconda 2.3.0 (x86_64) on Mac OS X 10.11.4 15E65.

我在 Mac OS X 10.11.4 15E65 上使用 Python 2.7.11 :: Anaconda 2.3.0 (x86_64)。

You may want to follow the steps below:

您可能需要按照以下步骤操作:

  • Install homebrew
  • Open a terminal and run: brew install mysql-connector-c
  • pip install mysql-python
  • 安装自制软件
  • 打开终端并运行: brew install mysql-connector-c
  • pip 安装 mysql-python

Then the Anaconda will have the mysql-python installed and you can start with MySQLdb then.

然后 Anaconda 将安装 mysql-python,然后您可以从 MySQLdb 开始。

Good luck. Thanks.

祝你好运。谢谢。

回答by The Aelfinn

For Python 3+the mysql-pythonlibrary is broken. Instead, use the mysqlclient library. Install with: pip install mysqlclient

对于 Python 3+,mysql-python库已损坏。相反,使用mysqlclient 库Install with: pip install mysqlclient

It is a fork of mysql-python (also known as MySQLdb) that supports Python 3+

它是支持 Python 3+的 mysql-python(也称为 MySQLdb)的一个分支

This library talks to the MySQL client's C-interface, and is faster than the pure-python pymysql libray.

这个库与 MySQL 客户端的 C 接口通信,并且比纯 python pymysql 库更快。

Note: you will need the mysql-developer tools installed. An easy way to do this on a Mac is to run

注意:您需要安装 mysql-developer 工具。在 Mac 上执行此操作的一种简单方法是运行

brew install mysql-connector-c

to delegate this task to homebrew. If you are on linux, you can install these via the instructions at the mysqlclient github page.

将此任务委托给自制软件。如果您使用的是 linux,则可以通过mysqlclient github 页面上的说明安装这些。

回答by Golden Thumb

What worked for me is:

对我有用的是:

LDFLAGS=-L/usr/local/opt/openssl/lib pip install mysql-python