Windows 環(huán)境下安裝python包經(jīng)常會(huì)有各種環(huán)境方面的問(wèn)題及報(bào)錯(cuò)公黑。 最近在python36/37下升級(jí)安裝mysqlclient包時(shí)產(chǎn)生了下面的錯(cuò)誤须尚,不論是百度還是Stack Overflow上查找都沒(méi)有真正的解決方案号胚,于是寫(xiě)文章與大家一起分享下這類報(bào)錯(cuò)的解決方法。
報(bào)錯(cuò)信息:
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: 無(wú)法打開(kāi)包括文件: “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: 無(wú)法打開(kāi)包括文件: “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\
原因排查
此類問(wèn)題需要真正的會(huì)看且有耐心去看報(bào)錯(cuò)信息,根據(jù)報(bào)錯(cuò)去分析和思考問(wèn)題出在哪個(gè)環(huán)節(jié)上声怔,然后一步一步縮減范圍,鎖定問(wèn)題原因醋火。cl.exe' failed with exit status 2
基本上都是因?yàn)?strong>編譯過(guò)程中缺少必要的依賴庫(kù)文件所導(dǎo)致的。缺少哪個(gè)依賴文件芥驳,上面的報(bào)錯(cuò)信息就會(huì)指出來(lái)—— _mysql.c(29): fatal error C1083: 無(wú)法打開(kāi)包括文件: “mysql.h”: No such file or directory
。 所以此次報(bào)錯(cuò)的原因就出來(lái)了兆旬,缺少了 mysql.h 這個(gè)頭文件。
當(dāng)把mysql.h添加進(jìn)依賴庫(kù)后丽猬,還會(huì)報(bào) [ 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 ]。
所以其核心原因在于環(huán)境的依賴庫(kù)中不存在mysql相關(guān)的文件脚祟。去MySQL官方下載個(gè)MySQL-Server包[https://dev.mysql.com/downloads/mysql/?product=community],zip或者installer均可
由桌,如果本機(jī)上已經(jīng)有MySQL-Server也行,到圖中MySQL-Server目錄中include中拷貝圖中選中的文件及文件夾到依賴庫(kù)目錄中去沥寥。
依賴庫(kù)目錄在哪?
上面的報(bào)錯(cuò)信息我們從下往上倒推邑雅,在“mysql.h”: No such file or directory
這行往上第二行,報(bào)錯(cuò)中的 -IC:\Program Files (x86)\MySQL\MySQL Connector C 6.1\include
淮野, 在每一個(gè) -I
后面緊跟的就是系統(tǒng)會(huì)查找的庫(kù)目錄捧书。只要其中任何一個(gè)路徑下能找到報(bào)錯(cuò)中需要依賴的頭文件就能讓安裝順利進(jìn)行下去吹泡。
雖然python的安裝目錄下的include文件夾也可以存放這些依賴文件,但如果你的系統(tǒng)中安裝有多個(gè)不同的python版本经瓷,那么為了避免復(fù)制多分這些依賴文件爆哑,還是建議放到其它目錄中,這樣不同Python版本下安裝都沒(méi)有問(wèn)題舆吮。
大功告成了揭朝?
只差最后一步了。
以上的依賴庫(kù)文件添加完之后色冀,還會(huì)再依賴一個(gè)靜態(tài)類庫(kù)文件潭袱。這個(gè)文件在 MySQL-Server/lib
中。
# 只截取以下關(guān)鍵錯(cuò)誤信息貼出
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
根據(jù)以上報(bào)錯(cuò)锋恬,將 mysqlclient.lib
文件從MySQL-Server那里拷貝到任意一個(gè) /LIBPATH
即可屯换!
OK.
總結(jié)
雖然今天對(duì)于我們來(lái)說(shuō)只是mysqlclient這個(gè)包的安裝問(wèn)題,但其實(shí)還有很多網(wǎng)友在搜索許多類似原因?qū)е碌陌鼰o(wú)法正常安裝的問(wèn)題与学,比如cudarray, pyltp, Scrapy等等彤悔,相信這篇文章一樣可以幫助他們找到解決方案。
其它
在Stack Overflow上有網(wǎng)友提供了一個(gè)較為省事兒的解決辦法索守,直接下載現(xiàn)成的.whl文件到本地進(jìn)行安裝(資源地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/)晕窑。