简体   繁体   English

安装MySQL-python时出错

[英]Error while installing MySQL-python

I'm using Python 2.7 and Django 1.9.4. 我正在使用Python 2.7和Django 1.9.4。 I want to integrate my Django application with MySQL 5.7 database and I executed: 我想将Django应用程序与MySQL 5.7数据库集成,然后执行:

pip install MySQL-python

on Windows 7 machine 64-bit. 在Windows 7计算机64位上。

And getting Error : 并得到错误:

    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\

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

You need a C compiler on your system. 您的系统上需要一个C编译器。 The easiest way is to use the on Microsoft provides. 最简单的方法是使用Microsoft提供的。

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

About Python : 关于Python:

First check is python running properly in command prompt. 首先检查python是否在命令提示符下正常运行。

If not Download and install python 2.7. 如果没有,请下载并安装python 2.7。

Once it is downloaded, you generally have to go to Control Panel > System > Advanced System Settings > Advanced Tab > Environment Variables > Path > Edit. 下载后,通常必须转到“控制面板”>“系统”>“高级系统设置”>“高级选项卡”>“环境变量”>“路径”>“编辑”。 Here you need to add the Windows path of your Python installation. 在这里,您需要添加Python安装的Windows路径。 As an example, mine is “C:\\Python27”. 例如,我的是“ C:\\ Python27”。 If you don't add this to the Path variable, you won't be able to run Python from the command line and it'll be annoying to test things. 如果不将其添加到Path变量中,则将无法从命令行运行Python,这将使测试变得烦人。

Install pip : 安装pip:

Next, we should install pip, the Python package manager (some of this section is from experience some from this stackoverflow page). 接下来,我们应该安装Python软件包管理器pip(本节的某些内容来自于该stackoverflow页面的一些经验)。 In case you were wondering, Python 3.4 already has pip installed with it! 如果您想知道,Python 3.4已经安装了pip! But if you don't have it there are a few options. 但是,如果没有它,则有一些选择。 If you have easy_install on your machine, you can simply navigate to the Python27\\Scripts directory and type: 如果您的计算机上具有easy_install,则只需导航至Python27 \\ Scripts目录并键入:

easy_install.exe pip

This should install pip for you. 这应该为您安装pip。 If you don't have easy_install, you can download a python script from here . 如果您没有easy_install,则可以从此处下载python脚本。 Make sure to save that get-pip.py file as a .py file not .txt. 确保将get-pip.py文件另存为.py文件而不是.txt。 You can then run the script: 然后,您可以运行脚本:

 python get-pip.py

Once pip is installed, you can add the directory where pip is to your path like we did before (probably something like “C:\\Python27\\Scripts”). 一旦安装了pip,就可以像以前一样将pip所在的目录添加到您的路径中(可能类似于“ C:\\ Python27 \\ Scripts”)。

About MySQL: 关于MySQL:

pip install mysql-python

You may get the error Unable to find vcvarsall.bat either when installing pip or other packages (I got it from trying to install mysql-python package). 在安装pip或其他软件包时,您可能会收到错误“无法找到vcvarsall.bat ”(我是从尝试安装mysql-python软件包得到的)。 This means Windows can't find a C/C++ compiler (it's looking for Visual Studio) which pip uses to install packages and install itself. 这意味着Windows找不到pip用于安装软件包和自行安装的C / C ++编译器(正在寻找Visual Studio)。

Finally test it : 最后测试一下:

You can check if mysql-python is correctly installed by going to the command prompt and typing: 您可以通过转到命令提示符并键入以下命令来检查mysql-python是否已正确安装:

C:\> python
>>>import MySQLdb

Done with alot of research, even downloaded unofficial windows binaries for python extensions - again made disappointment. 做了大量研究,甚至下载了用于python扩展的非官方Windows二进制文件-再次令人失望。 Finally, after trying simply following command and it worked. 最后,在尝试仅遵循以下命令后,它就起作用了。

easy_install mysql-python

Best way is to install mysqlclient in the case of getting errors in installing mysql-python . 最好的方法是在安装mysql-python出错时安装mysqlclient
mysqlclient is a fork of it and hence it can be used as a substitute and it contains MySQLdb aswell. mysqlclient是它的一个分支,因此它可以用作替代,并且还包含MySQLdb So, better go for it 所以,最好去做

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

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