Windows Python2.7 mysqldb installation error

2020-03-12 05:55发布

问题:

I'm trying to install mysqldb for Python. I'm running "pip install mysql-python" and I keep getting this error:

running build_ext
building '_mysql' extension
creating build\temp.win32-2.7
creating build\temp.win32-2.7\Release
C:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -Dversion_info=(1,2,5,'fi nal',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_mysql.c /Fobui ld\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:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe' failed with exit status 2

----------------------------------------
Failed building wheel for mysql-python
Failed to build mysql-python
Installing collected packages: mysql-python
Running setup.py install for mysql-python
Complete output from command c:\python27\python.exe -c "import setuptools, tokenize;file='c:\users\scott~1.sco\appdata\local\temp\pip-bu ild-nja4gr\mysql-python\setup.py';exec(compile(getattr(tokenize, 'open', open)(file).read().replace('\r\n', '\n'), file, 'exec'))" install - -record c:\users\scott~1.sco\appdata\local\temp\pip-5htk1y-record\install-record.txt --single-version-externally-managed --compile: running install
running build
running build_py
copying MySQLdb\release.py -> build\lib.win32-2.7\MySQLdb
running build_ext
building '_mysql' extension
C:\Program Files (x86)\Common Files\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_mysql.c /Fob uild\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:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe' failed with exit status 2

----------------------------------------

Command "c:\python27\python.exe -c "import setuptools, tokenize;file='c:\users\scott~1.sco\appdata\local\temp\pip-build-nja4gr\mysql-python \setup.py';exec(compile(getattr(tokenize, 'open', open)(file).read().replace('\r\n', '\n'), file, 'exec'))" install --record c:\users\scott~1 .sco\appdata\local\temp\pip-5htk1y-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in c:\users\scott ~1.sco\appdata\local\temp\pip-build-nja4gr\mysql-python

Can anyone tell me whats the problem?

回答1:

Download the pre-compiled whl file for mysql-python from from Christophe Gohlke’s Library of pre-compiled python packages for windows from the below location.

https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

Now got to directory where you downloaded that file and open command prompt there.

Now run below command pip install xxx.whl

Best Luck !!



回答2:

You can try...

   pip install mysqlclient==1.3.4

It worked for me.

If the above command doesn't work try this...

 pip install --only-binary :all: mysqlclient

both worked for me.



回答3:

I got a same error.

I solved it by install mysql-connector



回答4:

You can get a pre-compiled version from Christophe Gohlke’s Library of pre-compiled python packages for windows. Specifically, you can find the MySQL_python package here

Other alternatives would be to use a python distribution that comes with many packages pre-compiled and available for easy installation. Two examples are:

  • Anaconda Python Distribution
  • Enthought Canopy Python Distribution


回答5:

You are missing Visual Studio C++ from your system or the correct environment for VS is not set properly based on the error in your question: "error: command 'C:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe' failed with exit status 2"

Please check if you have C++ compiler installed at "C:\Program Files (x86)\Common Files\Microsoft\Visual C++ for Python\9.0\VC\Bin\cl.exe". If you do not have this installed it is available from Microsoft here: python27 Compiler

If you have VS installed try running the pip install from visual studio command prompt ,which generally would have all the environment created or try to run vcvarsall.bat from "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\" folder



回答6:

Simply go to the site:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

and download mysqlclient wheel file according to your python version and operating system bit specification, then install the downloaded file with typing

pip install downloaded_whl_file_name

Finally install mysqldb with command:

pip install flask-mysqldb


回答7:

In my case I had to install mysql-connector with the same bitness as my python. So although I run 64bit windows 10, I have 32bit python 2.7 installed so I had to install 32bit mysql-connector from here to overcome the error.



回答8:

The following action, mentioned above by zio, has solved my problem:

You can get a pre-compiled version from Christophe Gohlke’s Library of pre-compiled python packages for windows. Specifically, you can find the MySQL_python package here. After get the file, install it using pip install.