欢迎食用『主界面』~,这里是赵苦瓜的看板娘desu~

#
已经安装了Visual Studio但是仍然报错Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
首页 > 日常    作者:赵苦瓜   2024年4月15日 15:58 星期一   热度:242°   百度已收录  
时间:2024-4-15 15:58   热度:242° 

最近Python装包的时候又遇到了需要编译的C包,然后之前我的Visual Studio的安装环境炸了导致Visual Studio Installer找不到已经安装并且可以正常使用的Visual Studio。

因为Visual Studio的安装、添加模块、卸载、修复等操作都会调用Installer,所以如果Installer检测不到已经安装的Visual Studio这些操作都无法完成!


众所周知这是一个十分庞大的软件,我不想拿第三方工具完全卸载再重装,于是我开始探索为什么会出现这个报错。


我安的包使用了setuptools进行安装,所以玄机很有可能就藏在这里面。很巧,setuptools里面刚好有一个msvc.py文件,里面刚好有这条报错的提示。69.5.1版本的在264行左右。

def _augment_exception(exc, version, arch=''):
    """
    Add details to the exception message to help guide the user
    as to what action will resolve it.
    """
    # Error if MSVC++ directory not found or environment not set
    message = exc.args[0]
    
    
    if "vcvarsall" in message.lower() or "visual c" in message.lower():
        # Special error message if MSVC++ not installed
        tmpl = 'Microsoft Visual C++ {version:0.1f} or greater is required.'
        message = tmpl.format(**locals())
        msdownload = 'www.microsoft.com/download/details.aspx?id=%d'
        if version == 9.0:
            if arch.lower().find('ia64') > -1:
                # For VC++ 9.0, if IA64 support is needed, redirect user
                # to Windows SDK 7.0.
                # Note: No download link available from Microsoft.
                message += ' Get it with "Microsoft Windows SDK 7.0"'
            else:
                # For VC++ 9.0 redirect user to Vc++ for Python 2.7 :
                # This redirection link is maintained by Microsoft.
                # Contact vspython@microsoft.com if it needs updating.
                message += ' Get it from http://aka.ms/vcpython27'
        elif version == 10.0:
            # For VC++ 10.0 Redirect user to Windows SDK 7.1
            message += ' Get it with "Microsoft Windows SDK 7.1": '
            message += msdownload % 8279
        elif version >= 14.0:
            # For VC++ 14.X Redirect user to latest Visual C++ Build Tools
            message += (
                ' Get it with "Microsoft C++ Build Tools": '
                r'https://visualstudio.microsoft.com'
                r'/visual-cpp-build-tools/'
            )

    exc.args = (message,)


可以看到这就是报错的来源。

但是怎么确定是谁调用了这个函数的呢?

首先我print了最开始他用于判断错误的message,发现是Unable to find vcvarsall.bat。

我又导入了inspect模块来查看函数调用堆栈,发现恰好是上面一个函数msvc14_get_vc_env

而msvc14_get_vc_env又调用了_msvc14_get_vc_env,恰好也在上面。

然后刚好刚才的错误信息Unable to find vcvarsall.bat也在这里,说明是_msvc14_find_vcvarsall返回的vcvarsall是None

在_msvc14_find_vcvarsall中打印了一下best_dir发现也是None,说明_msvc14_find_vc2017获取best_dir失败了

在_msvc14_find_vc2017终于水落石出。大概在73行左右。

这个函数里执行了一个命令vswhere.exe -latest -prerelease -requires "Microsoft.VisualStudio.Component.VC.Tools.x86.x64" -property installationPath -products *

他是调用Visual Studio Installer里面的一个vswhere.exe来查找已经安装的Visual Studio路径的!

正常情况下调用这个查找路径会返回C:\Program Files\Microsoft Visual Studio\2022\Community这种路径,但是Installer自己都找不到已经安装的VS,更别提别的软件拿他找了!


那解决方法就很简单了,一个是改setuotools的msvc.py文件,把103行左右刚从vswhere.exe获取的空路径path替换成你的实际路径。当然这还是不太方便,包一更新或者换版本就寄了。

还可以直接替换掉原来的vswhere.exe,反正之前都找不到,现在不如直接让他无论怎么样都直接返回你的实际路径,C语言写一个printf("路径")编译成exe替换进去得了。


Microsoft Windows [版本 10.0.19045.4291]
(c) Microsoft Corporation。保留所有权利。

C:\Program Files (x86)\Microsoft Visual Studio\Installer>vswhere.exe
C:\Program Files\Microsoft Visual Studio\2022\Community
C:\Program Files (x86)\Microsoft Visual Studio\Installer>





#include<iostream>
using namespace std;


int main()
{
	printf("C:\\Program Files\\Microsoft Visual Studio\\2022\\Community");
	return 0;
}





本文作者:赵苦瓜      文章标题: 已经安装了Visual Studio但是仍然报错Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
本文地址:https://blog.jixiaob.cn/?post=114
版权声明:若无注明,本文皆为“赵苦瓜のBlog~”原创,转载请保留文章出处。
https://autopatchcn.yuanshen.com/client_app/update/hk4e_cn/18/game_4.5.0_4.6.0_hdiff_kiOtCmQKDxTEojvz.zip好

返回顶部    首页    后花园  
版权所有:赵苦瓜のBlog~    站长: 赵苦瓜    程序:emlog   鲁ICP备20030743号-1   鲁公网安备37048102006726 萌ICP备20222268号    sitemap