Windows11使用Cpython 编译文件报错 error: Unable to find vcvarsall.bat 完美解决方法

这篇文章主要介绍了Windows11使用Cpython编译文件报错error:Unable to find vcvarsall.bat完美解决方法,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

开发环境说明:
python 3.6.2
Vs studio 2017 (已经安装C++桌面开发)

我的vcvarsall.bat 路径为:

"D:\vsstudio\VC\Auxiliary\Build\vcvarsall.bat"

一般在Vs studio 的此安装路径下

修改python源代码
修改文件为 python3.6.2\Lib\distutils_msvccompiler.py 注意 前面存在下划线:

我的文件路径为:

"D:\core_package\python3.6.2\Lib\distutils_msvccompiler.py"

接下来 使用记事本打开:

将_find_vcvarsall 函数修改:
源代码:

def _find_vcvarsall(plat_spec):
    try:
        key = winreg.OpenKeyEx(
            winreg.HKEY_LOCAL_MACHINE,
            r"Software\Microsoft\VisualStudio\SxS\VC7",
            access=winreg.KEY_READ | winreg.KEY_WOW64_32KEY
        )
    except OSError:
        log.debug("Visual C++ is not registered")
        return None, None
    with key:
        best_version = 0
        best_dir = None
        for i in count():
            try:
                v, vc_dir, vt = winreg.EnumValue(key, i)
            except OSError:
                break
            if v and vt == winreg.REG_SZ and os.path.isdir(vc_dir):
                try:
                    version = int(float(v))
                except (ValueError, TypeError):
                    continue
                if version >= 14 and version > best_version:
                    best_version, best_dir = version, vc_dir
        if not best_version:
            log.debug("No suitable Visual C++ version found")
            return None, None
        vcvarsall = os.path.join(best_dir, "vcvarsall.bat")
        if not os.path.isfile(vcvarsall):
            log.debug("%s cannot be found", vcvarsall)
            return None, None
        vcruntime = None
        vcruntime_spec = _VCVARS_PLAT_TO_VCRUNTIME_REDIST.get(plat_spec)
        if vcruntime_spec:
            vcruntime = os.path.join(best_dir,
                vcruntime_spec.format(best_version))
            if not os.path.isfile(vcruntime):
                log.debug("%s cannot be found", vcruntime)
                vcruntime = None
        return vcvarsall, vcruntime

修改为:

def _find_vcvarsall(plat_spec):
    best_dir = r"D:\vsstudio\VC\Auxiliary\Build"
    best_version = 17
    vcruntime = None
    vcruntime_spec = _VCVARS_PLAT_TO_VCRUNTIME_REDIST.get(plat_spec)
    if vcruntime_spec:
        vcruntime = os.path.join(best_dir,
                                 vcruntime_spec.format(best_version))
        if not os.path.isfile(vcruntime):
            log.debug("%s cannot be found", vcruntime)
            vcruntime = None
    print(vcruntime)
    return r"D:\vsstudio\VC\Auxiliary\Build\vcvarsall.bat", vcruntime

要修改的 如下图所示:

如果跟我配置一样的话 到指定目录终端下

就可以输入

python setup.py build_ext --inplace

完成 PYTHON 到 c 的文件编译啦

成功截图 如下所示:

注意 编译完成后要使用时 需要将 .pyx文件 .c文件 .py文件全部删除 只保留 pyd文件

在其他文件调用时 不用担心报错 可以正常运行

输出结果如下所示

到此这篇关于Windows11使用Cpython 编译文件报错 error: Unable to find vcvarsall.bat 完美解决方法的文章就介绍到这了,更多相关Cpython 编译文件报错内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持vb.net教程C#教程python教程SQL教程access 2010教程xin3721自学网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值