安装MySQL-python时出错

时间:2017-01-13 06:17:41

标签: python mysql pip

我正在使用Python 2.7和Django 1.9.4。我想将我的Django应用程序与MySQL 5.7数据库集成并执行:

pip install MySQL-python

在Windows 7计算机64位上。

得到错误:

    F:\Djangoproject\mysite>pip install MySQL-python
Collecting MySQL-python
  Using cached MySQL-python-1.2.5.zip
Building wheels for collected packages: MySQL-python
  Running setup.py bdist_wheel for MySQL-python ... error
  Complete output from command c:\python27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\chandra\\appdata\\local\\temp\\pip-build
-3qbbxo\\MySQL-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__
, 'exec'))" bdist_wheel -d c:\users\chandra\appdata\local\temp\tmpoytuplpip-wheel- --python-tag cp27:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win32-2.7
  copying _mysql_exceptions.py -> build\lib.win32-2.7
  creating build\lib.win32-2.7\MySQLdb
  copying MySQLdb\__init__.py -> build\lib.win32-2.7\MySQLdb
  copying MySQLdb\converters.py -> build\lib.win32-2.7\MySQLdb
  copying MySQLdb\connections.py -> build\lib.win32-2.7\MySQLdb
  copying MySQLdb\cursors.py -> build\lib.win32-2.7\MySQLdb
  copying MySQLdb\release.py -> build\lib.win32-2.7\MySQLdb
  copying MySQLdb\times.py -> build\lib.win32-2.7\MySQLdb
  creating build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\__init__.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\CR.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\ER.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\FLAG.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\REFRESH.py -> build\lib.win32-2.7\MySQLdb\constants
  copying MySQLdb\constants\CLIENT.py -> build\lib.win32-2.7\MySQLdb\constants
  running build_ext
  building '_mysql' extension
  creating build\temp.win32-2.7
  creating build\temp.win32-2.7\Release
  C:\Users\Chandra\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info
=(1,2,5,'final',1) -D__version__=1.2.5 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include" -Ic:\python27\include -Ic:\python27\PC /Tc_mys
ql.c /Fobuild\temp.win32-2.7\Release\_mysql.obj /Zl
  _mysql.c
  _mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
  error: command 'C:\\Users\\Chandra\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit statu
s 2

  ----------------------------------------
  Failed building wheel for MySQL-python
  Running setup.py clean for MySQL-python
Failed to build MySQL-python
Installing collected packages: MySQL-python
  Running setup.py install for MySQL-python ... error
    Complete output from command c:\python27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\chandra\\appdata\\local\\temp\\pip-bui
ld-3qbbxo\\MySQL-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file
__, 'exec'))" install --record c:\users\chandra\appdata\local\temp\pip-rskyc7-record\install-record.txt --single-version-externally-managed --compile:

    running install
    running build
    running build_py
    creating build
    creating build\lib.win32-2.7
    copying _mysql_exceptions.py -> build\lib.win32-2.7
    creating build\lib.win32-2.7\MySQLdb
    copying MySQLdb\__init__.py -> build\lib.win32-2.7\MySQLdb
    copying MySQLdb\converters.py -> build\lib.win32-2.7\MySQLdb
    copying MySQLdb\connections.py -> build\lib.win32-2.7\MySQLdb
    copying MySQLdb\cursors.py -> build\lib.win32-2.7\MySQLdb
    copying MySQLdb\release.py -> build\lib.win32-2.7\MySQLdb
    copying MySQLdb\times.py -> build\lib.win32-2.7\MySQLdb
    creating build\lib.win32-2.7\MySQLdb\constants
    copying MySQLdb\constants\__init__.py -> build\lib.win32-2.7\MySQLdb\constants
    copying MySQLdb\constants\CR.py -> build\lib.win32-2.7\MySQLdb\constants
    copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win32-2.7\MySQLdb\constants
    copying MySQLdb\constants\ER.py -> build\lib.win32-2.7\MySQLdb\constants
    copying MySQLdb\constants\FLAG.py -> build\lib.win32-2.7\MySQLdb\constants
    copying MySQLdb\constants\REFRESH.py -> build\lib.win32-2.7\MySQLdb\constants
    copying MySQLdb\constants\CLIENT.py -> build\lib.win32-2.7\MySQLdb\constants
    running build_ext
    building '_mysql' extension
    creating build\temp.win32-2.7
    creating build\temp.win32-2.7\Release
    C:\Users\Chandra\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_in
fo=(1,2,5,'final',1) -D__version__=1.2.5 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include" -Ic:\python27\include -Ic:\python27\PC /Tc_m
ysql.c /Fobuild\temp.win32-2.7\Release\_mysql.obj /Zl
    _mysql.c
    _mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
    error: command 'C:\\Users\\Chandra\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit sta
tus 2

    ----------------------------------------
Command "c:\python27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\chandra\\appdata\\local\\temp\\pip-build-3qbbxo\\MySQL-python\
\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --
record c:\users\chandra\appdata\local\temp\pip-rskyc7-record\install-record.txt --single-version-externally-managed --compile" failed with error code
1 in c:\users\chandra\appdata\local\temp\pip-build-3qbbxo\MySQL-python\

5 个答案:

答案 0 :(得分:0)

您的系统上需要C编译器。最简单的方法是使用Microsoft提供的。

https://www.microsoft.com/en-au/download/details.aspx?id=44266

答案 1 :(得分:0)

关于Python:

首先检查python是否在命令提示符下正常运行。

如果没有,请下载并安装python 2.7。

下载后,您通常必须转到控制面板>系统>高级系统设置>高级选项卡>环境变量>路径>编辑。在这里,您需要添加Python安装的Windows路径。举个例子,我的是“C:\ Python27”。如果你不将它添加到Path变量,你将无法从命令行运行Python,并且测试事情会很烦人。

安装点子

接下来,我们应该安装pip,Python包管理器(本节的部分内容来自此stackoverflow页面的一些经验)。万一你想知道,Python 3.4已经安装了pip!但如果你没有它,有一些选择。如果您的计算机上有easy_install,您只需导航到Python27 \ Scripts目录并输入:

easy_install.exe pip

这应该为你安装点子。如果您没有easy_install,可以从here下载python脚本。确保将get-pip.py文件保存为.py文件而不是.txt。然后,您可以运行脚本:

 python get-pip.py

一旦安装了pip,就可以像往常一样将pip所在的目录添加到你的路径中(可能类似于“C:\ Python27 \ Scripts”)。

关于MySQL:

pip install mysql-python

您可能会收到错误在安装pip或其他软件包时无法找到vcvarsall.bat(我从尝试安装mysql-python软件包获得它)。这意味着Windows无法找到一个C / C ++编译器(它正在寻找Visual Studio),pip用它来安装软件包并自行安装。

最后测试一下:

您可以通过转到命令提示符并键入:

来检查是否正确安装了mysql-python
C:\> python
>>>import MySQLdb

答案 2 :(得分:0)

最简单的方法是使用预编译的python包,可在此处找到http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

答案 3 :(得分:-1)

做了很多研究,甚至下载了用于python扩展的非官方Windows二进制文件-再次令人失望。 最后,在尝试仅遵循以下命令后,它就起作用了。

easy_install mysql-python

答案 4 :(得分:-1)

最好的方法是在安装mysqlclient时出错时安装mysql-python
mysqlclient是它的一个分支,因此可以用作替代,它也包含MySQLdb。所以,最好去做