tag:python
, visual c++
, pyhanlp
, jpype
本文写于2020年9月10日,阅读时请注意时效性。
问题出现:
在使用命令pip isntall pyhanlp
安装pyhanlp
的过程中出现了如下的错误信息,类似的问题也会在安装mysql-python
、scrapy
时出现。在安装RUST语言的时候也会遇到类似的情况。
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' extensionerror: 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.
解决方法:
- 不安装Visual Studio的方法:
- 按照上面的链接打开Visual Studio的下载界面(目前是VS2019),找到Visual Studio 2019 工具(Tools for Visual Studio 2019)Visual Studio 2019 生成工具(Build Tools for Visual Studio 2019)
- 下载安装,系统中会出现Visual Studio Installer
- 选择单个组件选项卡,安装如图所示的两个组件即可
- 安装Visual Studio的方法:直接打开Visual Studio Installer,安装上图所示的两个组件即可
- Stack Overflow上还有另外一种方法——直接执行下面链接中给出的安装程序,但是方法中只给出了链接却并没有给出出处,所以不推荐这种方法。这种方法的原理应该与前两种类似,详情见下面的分析。原回答中的链接如下:http://go.microsoft.com/fwlink/?LinkId=691126&fixForIE=.exe.
解决过程和分析:
首先,要注意一个语言问题,build tools翻译成中文是生成工具,一开始以为应该是构建工具所以浪费了很多时间。
然后,如果已经安装Visual Studio的话其实没必要再安装一次生成工具了,因为生成工具中的内容已经在本体中包含了,如果同时安装生成工具和本体会出现下面这样的情况,图是别人的2017,看个意思就好。
[图片上传失败...(image-a9ba94-1599747624822)]
- 接下来,如果只安装单个组件中的MSVC v140 - VS 2015 C++ 生成工具(v14.00)是不行的,会报错误:fatal error C1083: 无法打开包括文件: “basetsd.h”: No such file or directory。
{太长前略}
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: 无法打开包括文件: “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.
- 这个问题实际上是由于没有安装Windows SDK引起的,但是如果追加安装Visual Studio 2019中的Windows 10 SDK依然无法解决,会报另一个错误”LINK : fatal error LNK1158: 无法运行“rc.exe”“:
{太长前略}
正在创建库 build\temp.win-amd64-3.8\Release\build\src_jpype.cp38-win_amd64.lib 和对象 build\temp.win-amd64-3.8\Release\build\src_jpype.cp38-win_amd64.exp
正在生成代码
已完成代码的生成
LINK : fatal error LNK1158: 无法运行“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.
参考了一些其他人的情况,这个错误是由于SDK的版本和Build Tools的版本不匹配造成的,只要SDK版本和相应的Build Tools版本匹配是不会出现这个问题的。根据我自己的尝试,混搭方法都失败了。至于什么叫做匹配,下面会提到。
-
其实在Build Tools for Visual Studio 2019的工作负载选项卡中选择C++ Build Tools,安装默认的5个组件就可以完美解决,只是后来抱着”能少一点是一点“的心态又试了几次才发现只要单独安装前两个组件就可以解决问题。
第三种方法中得到的安装包的详细信息中显示版本号
14.0.25420.1
,推测应该就是VS2015的生成工具,但是实在找不到链接的出处了。与上面的方法相似,默认安装选项直接解决,自定义安装时如果不选上Windows 8 SDK的话会报和上面一样的错误。匹配就是指像上图中的14.27对应Windows 10的高版本SDK而14.0对应下图中的Windows 8 SDK。
具有参考价值的Stack Overflow上的问题:
- https://stackoverflow.com/questions/29846087/microsoft-visual-c-14-0-is-required-unable-to-find-vcvarsall-bat
- https://stackoverflow.com/questions/40504552/how-to-install-visual-c-build-tools
- https://stackoverflow.com/questions/14372706/visual-studio-cant-build-due-to-rc-exe?newreg=f00ac10c2a4448ceb438f258e5c229a4
另外,Build Tools for Visual Studio 2019也可以从这个链接直接获得,这是RUST官网上给出的链接:https://visualstudio.microsoft.com/visual-cpp-build-tools/