python安裝jpype1、pyhanlp時(shí)出現(xiàn)的“Microsoft Visual C++ 14.0 is required.”問(wèn)題解決

tag:python, visual c++, pyhanlp, jpype
本文寫(xiě)于2020年9月10日,閱讀時(shí)請(qǐng)注意時(shí)效性。

問(wèn)題出現(xiàn):

在使用命令pip isntall pyhanlp安裝pyhanlp的過(guò)程中出現(xiàn)了如下的錯(cuò)誤信息,類(lèi)似的問(wèn)題也會(huì)在安裝mysql-python、scrapy時(shí)出現(xiàn)。在安裝RUST語(yǔ)言的時(shí)候也會(huì)遇到類(lèi)似的情況。

C:\Windows\system32>pip install pyhanlp -i https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pyhanlp
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/8f/99/13078d71bc9f77705a29f932359046abac3001335ea1d21e91120b200b21/pyhanlp-0.1.66.tar.gz (86 kB)
|████████████████████████████████| 86 kB 628 kB/s
Collecting jpype1==0.7.0
Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/63/784834e8a24ec2e1ad7f703c3dc6c6fb372a77cc68a2fdff916e18a4449e/JPype1-0.7.0.tar.gz (470 kB)
|████████████████████████████████| 470 kB 2.2 MB/s
Using legacy 'setup.py install' for pyhanlp, since package 'wheel' is not installed.
Using legacy 'setup.py install' for jpype1, since package 'wheel' is not installed.
Installing collected packages: jpype1, pyhanlp
Running setup.py install for jpype1 ... error
ERROR: Command errored out with exit status 1:
command: 'c:\program files\python38\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\raner\AppData\Local\Temp\pip-install-mamcaw85\jpype1\setup.py'"'"'; file='"'"'C:\Users\raner\AppData\Local\Temp\pip-install-mamcaw85\jpype1\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\raner\AppData\Local\Temp\pip-record-ed77_3sc\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\program files\python38\Include\jpype1'
cwd: C:\Users\raner\AppData\Local\Temp\pip-install-mamcaw85\jpype1
Complete output (52 lines):
c:\program files\python38\lib\distutils\dist.py:274: UserWarning: Unknown distribution option: 'use_scm_version'
warnings.warn(msg)
running install
running build
running build_py
creating build
creating build\lib.win-amd64-3.8
creating build\lib.win-amd64-3.8\jpype
copying jpype\beans.py -> build\lib.win-amd64-3.8\jpype
copying jpype\imports.py -> build\lib.win-amd64-3.8\jpype
copying jpype\nio.py -> build\lib.win-amd64-3.8\jpype
copying jpype\reflect.py -> build\lib.win-amd64-3.8\jpype
copying jpype\types.py -> build\lib.win-amd64-3.8\jpype
copying jpype_classpath.py -> build\lib.win-amd64-3.8\jpype
copying jpype_core.py -> build\lib.win-amd64-3.8\jpype
copying jpype_cygwin.py -> build\lib.win-amd64-3.8\jpype
copying jpype_darwin.py -> build\lib.win-amd64-3.8\jpype
copying jpype_gui.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jarray.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jboxed.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jclass.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jcollection.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jcomparable.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jcustomizer.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jexception.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jinit.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jio.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jobject.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jpackage.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jproxy.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jstring.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jtypes.py -> build\lib.win-amd64-3.8\jpype
copying jpype_jvmfinder.py -> build\lib.win-amd64-3.8\jpype
copying jpype_linux.py -> build\lib.win-amd64-3.8\jpype
copying jpype_pykeywords.py -> build\lib.win-amd64-3.8\jpype
copying jpype_windows.py -> build\lib.win-amd64-3.8\jpype
copying jpype_init_.py -> build\lib.win-amd64-3.8\jpype
running build_ext
running build_java
Using Jar cache
creating build\lib
creating build\lib\org
creating build\lib\org\jpype
creating build\lib\org\jpype\classloader
copying native\jars\org\jpype\classloader\JPypeClassLoader.class -> build\lib\org\jpype\classloader
copying native\jars\org.jpype.jar -> build\lib
running build_thunk
Building thunks
including thunk build\lib\org\jpype\classloader\JPypeClassLoader.class
including thunk build\lib\org.jpype.jar
building '_jpype' extension

error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/

ERROR: Command errored out with exit status 1: 'c:\program files\python38\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\raner\AppData\Local\Temp\pip-install-mamcaw85\jpype1\setup.py'"'"'; file='"'"'C:\Users\raner\AppData\Local\Temp\pip-install-mamcaw85\jpype1\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\raner\AppData\Local\Temp\pip-record-ed77_3sc\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\program files\python38\Include\jpype1' Check the logs for full command output.

解決方法:

  1. 不安裝Visual Studio的方法:
    1. 按照上面的鏈接打開(kāi)Visual Studio的下載界面(目前是VS2019),找到Visual Studio 2019 工具(Tools for Visual Studio 2019)\toVisual Studio 2019 生成工具(Build Tools for Visual Studio 2019)
    2. 下載安裝,系統(tǒng)中會(huì)出現(xiàn)Visual Studio Installer
    3. 選擇單個(gè)組件選項(xiàng)卡,安裝如圖所示的兩個(gè)組件即可
  2. 安裝Visual Studio的方法:直接打開(kāi)Visual Studio Installer,安裝上圖所示的兩個(gè)組件即可
  3. Stack Overflow上還有另外一種方法——直接執(zhí)行下面鏈接中給出的安裝程序,但是方法中只給出了鏈接卻并沒(méi)有給出出處,所以不推薦這種方法。這種方法的原理應(yīng)該與前兩種類(lèi)似,詳情見(jiàn)下面的分析。原回答中的鏈接如下:http://go.microsoft.com/fwlink/?LinkId=691126&fixForIE=.exe.

解決過(guò)程和分析:

  • 首先,要注意一個(gè)語(yǔ)言問(wèn)題,build tools翻譯成中文是生成工具,一開(kāi)始以為應(yīng)該是構(gòu)建工具所以浪費(fèi)了很多時(shí)間。

  • 然后,如果已經(jīng)安裝Visual Studio的話其實(shí)沒(méi)必要再安裝一次生成工具了,因?yàn)樯晒ぞ咧械膬?nèi)容已經(jīng)在本體中包含了,如果同時(shí)安裝生成工具和本體會(huì)出現(xiàn)下面這樣的情況,圖是別人的2017,看個(gè)意思就好。

[圖片上傳失敗...(image-a9ba94-1599747624822)]

  • 接下來(lái),如果只安裝單個(gè)組件中的MSVC v140 - VS 2015 C++ 生成工具(v14.00)是不行的,會(huì)報(bào)錯(cuò)誤:fatal error C1083: 無(wú)法打開(kāi)包括文件: “basetsd.h”: No such file or directory。

{太長(zhǎng)前略}
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -DWIN32=1 -Inative\common\include -Inative\python\include -Ibuild\src -Inative\jni_include "-Ic:\program files\python38\include" "-Ic:\program files\python38\include" "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE" "-IC:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt" /EHsc /Tpnative\common\jp_arrayclass.cpp /Fobuild\temp.win-amd64-3.8\Release\native\common\jp_arrayclass.obj /Zi /EHsc /std:c++14
jp_arrayclass.cpp
c:\program files\python38\include\pyconfig.h(206): fatal error C1083: 無(wú)法打開(kāi)包括文件: “basetsd.h”: No such file or directory

? error: command 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe' failed with exit status 2

ERROR: Command errored out with exit status 1: 'c:\program files\python38\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\raner\AppData\Local\Temp\pip-install-ogvytpx6\jpype1\setup.py'"'"'; file='"'"'C:\Users\raner\AppData\Local\Temp\pip-install-ogvytpx6\jpype1\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\raner\AppData\Local\Temp\pip-record-y9h_yg7p\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\program files\python38\Include\jpype1' Check the logs for full command output.

  • 這個(gè)問(wèn)題實(shí)際上是由于沒(méi)有安裝Windows SDK引起的,但是如果追加安裝Visual Studio 2019中的Windows 10 SDK依然無(wú)法解決,會(huì)報(bào)另一個(gè)錯(cuò)誤”LINK : fatal error LNK1158: 無(wú)法運(yùn)行“rc.exe”“:

{太長(zhǎng)前略}

正在創(chuàng)建庫(kù) build\temp.win-amd64-3.8\Release\build\src_jpype.cp38-win_amd64.lib 和對(duì)象 build\temp.win-amd64-3.8\Release\build\src_jpype.cp38-win_amd64.exp
正在生成代碼
已完成代碼的生成
LINK : fatal error LNK1158: 無(wú)法運(yùn)行“rc.exe”

error: command 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\link.exe' failed with exit status 1158

ERROR: Command errored out with exit status 1: 'c:\program files\python38\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\raner\AppData\Local\Temp\pip-install-qj00er_b\jpype1\setup.py'"'"'; file='"'"'C:\Users\raner\AppData\Local\Temp\pip-install-qj00er_b\jpype1\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\raner\AppData\Local\Temp\pip-record-5llbdh9e\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\program files\python38\Include\jpype1' Check the logs for full command output.

  • 參考了一些其他人的情況,這個(gè)錯(cuò)誤是由于SDK的版本和Build Tools的版本不匹配造成的,只要SDK版本和相應(yīng)的Build Tools版本匹配是不會(huì)出現(xiàn)這個(gè)問(wèn)題的。根據(jù)我自己的嘗試,混搭方法都失敗了。至于什么叫做匹配,下面會(huì)提到。

  • 其實(shí)在Build Tools for Visual Studio 2019工作負(fù)載選項(xiàng)卡中選擇C++ Build Tools,安裝默認(rèn)的5個(gè)組件就可以完美解決,只是后來(lái)抱著”能少一點(diǎn)是一點(diǎn)“的心態(tài)又試了幾次才發(fā)現(xiàn)只要單獨(dú)安裝前兩個(gè)組件就可以解決問(wèn)題。

  • 第三種方法中得到的安裝包的詳細(xì)信息中顯示版本號(hào)14.0.25420.1,推測(cè)應(yīng)該就是VS2015的生成工具,但是實(shí)在找不到鏈接的出處了。與上面的方法相似,默認(rèn)安裝選項(xiàng)直接解決,自定義安裝時(shí)如果不選上Windows 8 SDK的話會(huì)報(bào)和上面一樣的錯(cuò)誤。

  • 匹配就是指像上圖中的14.27對(duì)應(yīng)Windows 10的高版本SDK而14.0對(duì)應(yīng)下圖中的Windows 8 SDK。


具有參考價(jià)值的Stack Overflow上的問(wèn)題:

  1. https://stackoverflow.com/questions/29846087/microsoft-visual-c-14-0-is-required-unable-to-find-vcvarsall-bat
  2. https://stackoverflow.com/questions/40504552/how-to-install-visual-c-build-tools
  3. https://stackoverflow.com/questions/14372706/visual-studio-cant-build-due-to-rc-exe?newreg=f00ac10c2a4448ceb438f258e5c229a4

另外,Build Tools for Visual Studio 2019也可以從這個(gè)鏈接直接獲得,這是RUST官網(wǎng)上給出的鏈接:https://visualstudio.microsoft.com/visual-cpp-build-tools/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容