Windows Python3環境 pip安裝 mysqlclient包

Windows 環境下安裝python包常常會有各類環境方面的問題及報錯。 最近在python36/37下升級安裝mysqlclient包時產生了下面的錯誤,不管是百度仍是Stack Overflow上查找都沒有真正的解決方案,因而寫文章與你們一塊兒分享下這類報錯的解決方法。python

報錯信息:

Building wheels for collected packages: mysqlclient
  Running setup.py bdist_wheel for mysqlclient ... error
  Complete output from command "d:\program files\python\python36\python.exe" -u -c "import setuptools, tokenize;__file__='C:\\Users\\zdl25\\AppData\\Local\\Temp\\pip-install-83rodr64\\mysqlclient\\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\zdl25\AppData\Local\Temp\pip-wheel-nlo8hs6x --python-tag cp36:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win-amd64-3.6
  copying _mysql_exceptions.py -> build\lib.win-amd64-3.6
  creating build\lib.win-amd64-3.6\MySQLdb
  copying MySQLdb\__init__.py -> build\lib.win-amd64-3.6\MySQLdb
  copying MySQLdb\compat.py -> build\lib.win-amd64-3.6\MySQLdb
  copying MySQLdb\connections.py -> build\lib.win-amd64-3.6\MySQLdb
  copying MySQLdb\converters.py -> build\lib.win-amd64-3.6\MySQLdb
  copying MySQLdb\cursors.py -> build\lib.win-amd64-3.6\MySQLdb
  copying MySQLdb\release.py -> build\lib.win-amd64-3.6\MySQLdb
  copying MySQLdb\times.py -> build\lib.win-amd64-3.6\MySQLdb
  creating build\lib.win-amd64-3.6\MySQLdb\constants
  copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.6\MySQLdb\constants
  copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.6\MySQLdb\constants
  copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.6\MySQLdb\constants
  copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.6\MySQLdb\constants
  copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.6\MySQLdb\constants
  copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.6\MySQLdb\constants
  copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-3.6\MySQLdb\constants
  running build_ext
  building '_mysql' extension
  creating build\temp.win-amd64-3.6
  creating build\temp.win-amd64-3.6\Release
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,3,13,'final',0) -D__version__=1.3.13 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include" "-Id:\program files\python\python36\include" "-Id:\program files\python\python36\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\winrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\cppwinrt" /Tc_mysql.c /Fobuild\temp.win-amd64-3.6\Release\_mysql.obj /Zl
  _mysql.c
  _mysql.c(29): fatal error C1083: 沒法打開包括文件: 「mysql.h」: No such file or directory
  error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.15.26726\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2

  ----------------------------------------
  Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
  Found existing installation: mysqlclient 1.3.12
    Uninstalling mysqlclient-1.3.12:
      Successfully uninstalled mysqlclient-1.3.12
  Running setup.py install for mysqlclient ... error
    Complete output from command "d:\program files\python\python36\python.exe" -u -c "import setuptools, tokenize;__file__='C:\\Users\\zdl25\\AppData\\Local\\Temp\\pip-install-83rodr64\\mysqlclient\\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\zdl25\AppData\Local\Temp\pip-record-f544g5zo\install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.6
    copying _mysql_exceptions.py -> build\lib.win-amd64-3.6
    creating build\lib.win-amd64-3.6\MySQLdb
    copying MySQLdb\__init__.py -> build\lib.win-amd64-3.6\MySQLdb
    copying MySQLdb\compat.py -> build\lib.win-amd64-3.6\MySQLdb
    copying MySQLdb\connections.py -> build\lib.win-amd64-3.6\MySQLdb
    copying MySQLdb\converters.py -> build\lib.win-amd64-3.6\MySQLdb
    copying MySQLdb\cursors.py -> build\lib.win-amd64-3.6\MySQLdb
    copying MySQLdb\release.py -> build\lib.win-amd64-3.6\MySQLdb
    copying MySQLdb\times.py -> build\lib.win-amd64-3.6\MySQLdb
    creating build\lib.win-amd64-3.6\MySQLdb\constants
    copying MySQLdb\constants\__init__.py -> build\lib.win-amd64-3.6\MySQLdb\constants
    copying MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-3.6\MySQLdb\constants
    copying MySQLdb\constants\CR.py -> build\lib.win-amd64-3.6\MySQLdb\constants
    copying MySQLdb\constants\ER.py -> build\lib.win-amd64-3.6\MySQLdb\constants
    copying MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-3.6\MySQLdb\constants
    copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.6\MySQLdb\constants
    copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-3.6\MySQLdb\constants
    running build_ext
    building '_mysql' extension
    creating build\temp.win-amd64-3.6
    creating build\temp.win-amd64-3.6\Release
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Dversion_info=(1,3,13,'final',0) -D__version__=1.3.13 "-IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include" "-Id:\program files\python\python36\include" "-Id:\program files\python\python36\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\shared" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\um" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\winrt" "-IC:\Program Files (x86)\Windows Kits\8.1\include\\cppwinrt" /Tc_mysql.c /Fobuild\temp.win-amd64-3.6\Release\_mysql.obj /Zl
    _mysql.c
    _mysql.c(29): fatal error C1083: 沒法打開包括文件: 「mysql.h」: No such file or directory
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.15.26726\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2

    ----------------------------------------
  Rolling back uninstall of mysqlclient
Command ""d:\program files\python\python36\python.exe" -u -c "import setuptools, tokenize;__file__='C:\\Users\\zdl25\\AppData\\Local\\Temp\\pip-install-83rodr64\\mysqlclient\\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\zdl25\AppData\Local\Temp\pip-record-f544g5zo\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\zdl25\AppData\Local\Temp\pip-install-83rodr64\mysqlclient\

緣由排查

此類問題須要真正的會看且有耐心去看報錯信息,根據報錯去分析和思考問題出在哪一個環節上,而後一步一步縮減範圍,鎖定問題緣由。cl.exe' failed with exit status 2 基本上都是由於編譯過程當中缺乏必要的依賴庫文件所致使的。缺乏哪一個類庫文件,上面的報錯信息會有指出來—— _mysql.c(29): fatal error C1083: 沒法打開包括文件: 「mysql.h」: No such file or directory。 因此這次報錯的緣由就出來了,缺乏了 mysql.h 這個頭文件。mysql

當把mysql.h添加進類庫後,還會報 [ binary_log_types.h, errmsg.h, my_command.h, my_list.h, mysql_com.h, mysql_time.h, mysql_version.h, mysqld_error.h, mysql/client_plugin.h, mysql/plugin_auth_common.h, mysql/udf_registration_types.h ]。sql

因此其核心緣由在於環境的依賴庫中不存在mysql相關的文件。去MySQL官方下載個MySQL-Server包[https://dev.mysql.com/downloa...],zip或者installer都可
,若是本機上已經有MySQL-Server也行,到圖中MySQL-Server目錄中include中拷貝圖中選中的文件及文件夾到依賴庫目錄中去。api

mysqlclient類庫依賴的文件

什麼是依賴庫目錄?

上面的報錯信息咱們從下往上倒推,在「mysql.h」: No such file or directory這行往上第二行,報錯中的 -IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include, 在每個 -I 後面緊跟的就是系統會查找的庫目錄。只要其中任何一個路徑下能找到報錯中須要依賴的頭文件就能讓安裝順利進行下去。ui

雖然python的安裝目錄下的include文件夾也能夠存放這些依賴文件,但若是你的系統中安裝有多個不一樣的python版本,那麼爲了不復制多分這些依賴文件,仍是建議放到其它目錄中,這樣不一樣Python版本下安裝都沒有問題。spa

大功告成了?

只差最後一步了。code

以上的依賴庫文件添加完以後,還會再依賴一個靜態類庫文件。這個文件在 MySQL-Server/lib 中。blog

# 只截取如下關鍵錯誤信息貼出
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO "/LIBPATH:C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib\vs14" "/LIBPATH:D:\Program Files\Python\Python37\libs" "/LIBPATH:D:\Program Files\Python\Python37\PCbuild\amd64" "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\LIB\amd64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.10240.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\8.1\lib\winv6.3\um\x64" kernel32.lib advapi32.lib wsock32.lib mysqlclient.lib /EXPORT:PyInit__mysql build\temp.win-amd64-3.7\Release\_mysql.obj /OUT:build\lib.win-amd64-3.7\_mysql.cp37-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.7\Release\_mysql.cp37-win_amd64.lib /MANIFEST
LINK : fatal error LNK1181: cannot open input file 'mysqlclient.lib'
D:\Program Files\Python\Python37\lib\distutils\dist.py:274: UserWarning: Unknown distribution option: 'long_description_content_type'
  warnings.warn(msg)
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\link.exe' failed with exit status 1181

根據以上報錯,將 mysqlclient.lib 文件從MySQL-Server那裏拷貝到任意一個 /LIBPATH便可!
OK.token

總結

雖然今天對於咱們來講只是mysqlclient這個包的安裝問題,但其實還有不少網友在搜索許多相似緣由致使的包沒法正常安裝的問題,好比cudarray, pyltp, Scrapy等等,相信這篇文章同樣能夠幫助他們找到解決方案。ip

其它

在Stack Overflow上有網友提供了一個較爲省事兒的解決辦法,直接下載現成的.whl文件到本地進行安裝(資源地址: https://www.lfd.uci.edu/~gohl...)。
相關文章
相關標籤/搜索