在Windows中将MySQL与Python集成

问题:在Windows中将MySQL与Python集成

我发现在Windows系统中很难将MySQL与Python结合使用。

我目前正在使用Python 2.6。我试图使用提供的安装脚本来编译MySQL-python-1.2.3b1(应该适用于Python 2.6吗?)源代码。安装脚本将运行,并且不会报告任何错误,但不会生成_mysql模块。

我也尝试过为Python 2.5设置MySQL,但没有成功。使用2.5的问题是Python 2.5是使用Visual Studio 2003编译的(我使用提供的二进制文件安装了它)。我在Windows系统上安装了Visual Studio 2005。因此setuptools无法生成_mysql模块。

有什么帮助吗?

I am finding it difficult to use MySQL with Python in my windows system.

I am currently using Python 2.6. I have tried to compile MySQL-python-1.2.3b1 (which is supposed to work for Python 2.6 ?) source code using the provided setup scripts. The setup script runs and it doesn’t report any error but it doesn’t generate _mysql module.

I have also tried setting up MySQL for Python 2.5 with out success. The problem with using 2.5 is that Python 2.5 is compiled with visual studio 2003 (I installed it using the provided binaries). I have visual studio 2005 on my windows system. Hence setuptools fails to generate _mysql module.

Any help ?


回答 0

python-mysqldb的下载页面。该页面包含适用于Python 2.5、2.6和2.7的32位和64位版本的二进制文件。

也有关于消除弃用警告的讨论

更新:这是一个旧的答案。目前,我建议使用PyMySQL。它是纯python,因此它同样支持所有操作系统,几乎是mysqldb的直接替代品,并且还可以与python 3一起使用。最好的安装方法是使用pip。您可以从这里安装它(更多说明在此处),然后运行:

pip install pymysql

Download page for python-mysqldb. The page includes binaries for 32 and 64 bit versions of for Python 2.5, 2.6 and 2.7.

There’s also discussion on getting rid of the deprecation warning.

UPDATE: This is an old answer. Currently, I would recommend using PyMySQL. It’s pure python, so it supports all OSes equally, it’s almost a drop-in replacement for mysqldb, and it also works with python 3. The best way to install it is using pip. You can install it from here (more instructions here), and then run:

pip install pymysql

回答 1

这可能听起来像您的爷爷给我的建议,但是这里的所有答案都没有提到最好的方法:去安装ActivePython而不是python.org Windows二进制文件。很长一段时间以来,我真的想知道为什么在Windows上进行Python开发如此之薄-直到安装了activestate python。我不隶属于他们。这只是简单的事实。写在每堵墙上:Windows上的Python开发= ActiveState!然后您就可以pypm install mysql-python正常工作了。没有编译狂欢。没有奇怪的错误。没有恐怖。五分钟后就可以开始编码并进行实际工作。这是在Windows上运行的唯一方法。真。

This may read like your grandpa givin advice, but all answers here did not mention the best way: go nd install ActivePython instead of python.org windows binaries. I was really wondering for a long time why Python development on windows was such a pita – until I installed activestate python. I am not affiliated with them. It is just the plain truth. Write it on every wall: Python development on Windows = ActiveState! you then just pypm install mysql-python and everything works smoothly. no compile orgy. no strange errors. no terror. Just start coding and doing real work after five minutes. This is the only way to go on windows. Really.


回答 2

随着Python新手学习Python生态系统,我已经完成了这一步。

  1. 安装setuptools 说明

  2. 安装MySQL 5.1。从此处下载97.6MB MSI 您不能使用Essentials 版本,因为它不包含C库。
    确保选择一个自定义安装,并标记要安装的开发工具/库,因为默认情况下未完成安装。这是获取C头文件所必需的。
    您可以通过在安装目录中查找名为“ include”的文件夹来验证您是否已正确完成此操作。EG C:\ Program Files \ MySQL \ MySQL Server 5.1 \ include。它应该有一堆.h文件。

  3. 此处安装Microsoft Visual Studio C ++ Express 2008 这是获取C编译器所必需的。

  4. 以管理员身份打开命令行(右键单击Cmd快捷方式,然后“以管理员身份运行”。请确保打开一个新窗口 安装这些内容否则您的路径将不会更新,并且安装仍将失败。

  5. 在命令提示符下:

    easy_install -b C:\ temp \ sometempdir mysql-python

    那将失败-可以。

    现在,在您的临时目录C:\ temp \ sometempdir中打开site.cfg,并将“ registry_key”设置编辑为:

    Registry_key =软件\ MySQL AB \ MySQL Server 5.1

    现在将CD放入您的临时目录,并:

    python setup.py干净

    python setup.py安装

    您应该准备好摇滚!

  6. 是一个非常简单的脚本,可以根据需要开始为您学习Python DB API。

As Python newbie learning the Python ecosystem I’ve just completed this.

  1. Install setuptools instructions

  2. Install MySQL 5.1. Download the 97.6MB MSI from here You can’t use the essentials version because it doesnt contain the C libraries.
    Be sure to select a custom install, and mark the development tools / libraries for installation as that is not done by default. This is needed to get the C header files.
    You can verify you have done this correctly by looking in your install directory for a folder named “include”. E.G C:\Program Files\MySQL\MySQL Server 5.1\include. It should have a whole bunch of .h files.

  3. Install Microsoft Visual Studio C++ Express 2008 from here This is needed to get a C compiler.

  4. Open up a command line as administrator (right click on the Cmd shortcut and then “run as administrator”. Be sure to open a fresh window after you have installed those things or your path won’t be updated and the install will still fail.

  5. From the command prompt:

    easy_install -b C:\temp\sometempdir mysql-python

    That will fail – which is OK.

    Now open site.cfg in your temp directory C:\temp\sometempdir and edit the “registry_key” setting to:

    registry_key = SOFTWARE\MySQL AB\MySQL Server 5.1

    now CD into your temp dir and:

    python setup.py clean

    python setup.py install

    You should be ready to rock!

  6. Here is a super simple script to start off learning the Python DB API for you – if you need it.


回答 3

我发现一个人已经成功地为python2.6构建了mysql,并共享了链接,网址为http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe

…您可能会在导入MySQLdb时看到警告,这很正常,不会造成任何伤害,

C:\ Python26 \ lib \ site-packages \ MySQLdb__init __。py:34:DeprecationWarning:sets模块已从set import ImmutableSet中弃用

I found a location were one person had successfully built mysql for python2.6, sharing the link, http://www.technicalbard.com/files/MySQL-python-1.2.2.win32-py2.6.exe

…you might see a warning while import MySQLdb which is fine and that won’t hurt anything,

C:\Python26\lib\site-packages\MySQLdb__init__.py:34: DeprecationWarning: the sets module is deprecated from sets import ImmutableSet


回答 4

pymysql呢?它是纯Python,我已经在Windows上成功使用了它,绕过了编译和安装mysql-python的困难。

What about pymysql? It’s pure Python, and I’ve used it on Windows with considerable success, bypassing the difficulties of compiling and installing mysql-python.


回答 5

您不是唯一遇到Python 2.6和MySQL问题的人(http://blog.contriving.net/2009/03/04/using-python-26-mysql-on-windows-is-nearly-impossible/) 。这是说明如何在Python 2.5下运行的方法http://i.justrealized.com/2008/04/08/how-to-install-python-and-django-in-windows-vista/ 祝您好运

You’re not the only person having problems with Python 2.6 and MySQL (http://blog.contriving.net/2009/03/04/using-python-26-mysql-on-windows-is-nearly-impossible/). Here’s an explanation how it should run under Python 2.5 http://i.justrealized.com/2008/04/08/how-to-install-python-and-django-in-windows-vista/ Good luck


回答 6


回答 7

http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python上的预编译二进制文件仅对我有用。

  1. 打开 MySQL_python-1.2.5-cp27-none-win_amd64.whl使用zip提取程序文件。
  2. 将内容复制到 C:\Python27\Lib\site-packages\

The precompiled binaries on http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python is just worked for me.

  1. Open MySQL_python-1.2.5-cp27-none-win_amd64.whl file with zip extractor program.
  2. Copy the contents to C:\Python27\Lib\site-packages\

回答 8

在Python 3.4上,我已经从http://www.lfd.uci.edu/~gohlke/pythonlibs/安装了mysqlclient,并使用pip install mysqlclient进行了安装。

On Python 3.4 I’ve installed mysqlclient from http://www.lfd.uci.edu/~gohlke/pythonlibs/ with pip install mysqlclient and it’s working.


回答 9

您可以尝试使用myPySQL。真的很容易使用;Windows无需编译,即使您出于任何原因需要对其进行编译,也只需要安装Python和Visual C(而不是mysql)。

http://code.google.com/p/mypysql/

祝好运

You can try to use myPySQL. It’s really easy to use; no compilation for windows, and even if you need to compile it for any reason, you only need Python and Visual C installed (not mysql).

http://code.google.com/p/mypysql/

Good luck


回答 10

因为我在(pylons / pyramid)virtualenv中运行python,所以无法(有效地)运行以前链接的二进制安装程序。

我在执行Willie的回答步骤时遇到问题,但我确定问题是(可能)我正在运行Windows 7 x64 install,这将mysql的注册表项放在稍有不同的位置,特别是在我的情况下(请注意:正在运行5.5版):“ HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ MySQL AB \ MySQL Server 5.5”。

但是,路径中不能包含“ HKEY_LOCAL_MACHINE \”,否则它将失败。

另外,我必须在步骤3和4之间重新启动。

在完成所有这些工作之后,IMO可以从cygwin运行整个python dev环境会更聪明。

Because I am running python in a (pylons/pyramid) virtualenv, I could not run the binary installers (helpfully) linked to previously.

I had problems following the steps with Willie’s answer, but I determined that the problem is (probably) that I am running windows 7 x64 install, which puts the registry key for mysql in a slightly different location, specifically in my case (note: I am running version 5.5) in: “HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MySQL AB\MySQL Server 5.5”.

HOWEVER, “HKEY_LOCAL_MACHINE\” cannot be included in the path or it will fail.

Also, I had to do a restart between steps 3 and 4.

After working through all of this, IMO it would have been smarter to run the entire python dev environment from cygwin.


回答 11

如果您正在寻找Python 3.2,这似乎是我到目前为止找到的最佳解决方案

资料来源:http : //wiki.python.org/moin/MySQL

If you are looking for Python 3.2 this seems the best solution I found so far

Source: http://wiki.python.org/moin/MySQL


回答 12

您可能还想考虑使用Cygwin,它在存储库中具有mysql python库。

You might want to also consider making use of Cygwin, it has mysql python libraries in the repository.


回答 13

您还可以将pyodbcMySQL Connector / ODBC一起使用,以在Windows上使用MySQL。 Unixodbc也可用于使代码在Linux上兼容。Pyodbc使用标准的Python DB API 2.0,因此如果您坚持要在MySQL / PostgreSQL / SQLite / ODBC / JDBC驱动程序等之间进行切换,应该会比较轻松。

You can also use pyodbc with the MySQL Connector/ODBC to use MySQL on Windows. Unixodbc is also available to make the code compatible on Linux. Pyodbc uses the standard Python DB API 2.0 so if you stick with that switching between MySQL/PostgreSQL/SQLite/ODBC/JDBC drivers etc. should be relatively painless.


回答 14

赞成itsadok的答案,因为它也使我也安装了python 2.7,该软件位于:http : //www.codegood.com/archives/129

upvoted itsadok’s answer because it led me to the installation for python 2.7 as well, which is located here: http://www.codegood.com/archives/129


回答 15

厌倦了MySQLdb的安装问题,而是尝试了pymysql

易于安装;

git clone https://github.com/petehunt/PyMySQL.git
python setup.py install

API几乎相同。

Got sick of the installation troubles with MySQLdb and tried pymysql instead.

Easy setup;

git clone https://github.com/petehunt/PyMySQL.git
python setup.py install

And APIs are pretty much the same.