广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Windows11使用Cpython 编译文件报错 error: Unable to find vcvarsall.bat 完美解决方法
  • 143
分享到

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

Cpython 编译文件报错Unable to find vcvarsall.bat 2023-05-20 05:05:33 143人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录开发环境说明:修改python源代码开发环境说明: Python 3.6.2Vs studio 2017 (已经安装c++桌面开发) 我的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 编译文件报错内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

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

本文链接: https://www.lsjlt.com/news/213964.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作