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

作者:清虞 时间:2021-03-14 19:03:19 

开发环境说明:

  • python 3.6.2

  • Vs studio 2017 (已经安装C++桌面开发)

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

我的vcvarsall.bat 路径为:

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

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

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

修改python源代码

修改文件为 python3.6.2\Lib\distutils\_msvccompiler.py 注意 前面存在下划线:

我的文件路径为:

"D:\core_package\python3.6.2\Lib\distutils\_msvccompiler.py"

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

接下来 使用记事本打开:

  • 将_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

要修改的 如下图所示:

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

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

就可以输入

python setup.py build_ext --inplace

完成 PYTHON 到 c 的文件编译啦

成功截图 如下所示:

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

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

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

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

输出结果如下所示

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

来源:https://blog.csdn.net/qq_43647590/article/details/130618279

标签:Cpython,编译文件,报错
0
投稿

猜你喜欢

  • 浏览器针对单服务器连接数问题

    2008-05-12 22:27:00
  • python装饰器初探(推荐)

    2023-01-19 14:40:27
  • 如何将 awk 脚本移植到 Python

    2022-02-28 05:40:52
  • Python如何将控制台输出另存为日志文件

    2023-11-27 03:09:55
  • 数据库新手入门之MYSQL出错代码列表

    2008-12-23 15:22:00
  • Python实现通过文件路径获取文件hash值的方法

    2023-10-27 21:41:20
  • 简单获取键盘的KeyCode

    2008-04-18 12:37:00
  • python脚本定时发送邮件

    2023-08-07 16:36:02
  • ASP访问带多个参数的存储过程

    2008-10-14 16:45:00
  • Python中getpass模块无回显输入源码解析

    2022-05-02 03:58:28
  • PHP Laravel实现文件下载功能

    2023-11-18 12:20:27
  • Pytorch使用transforms

    2023-11-01 20:34:33
  • c++与python实现二分查找的原理及实现

    2021-11-23 21:09:06
  • python实现控制台打印的方法

    2021-12-18 12:21:04
  • python解决Fedora解压zip时中文乱码的方法

    2021-06-02 09:23:09
  • Python3 解决读取中文文件txt编码的问题

    2022-10-22 13:14:43
  • asp检测是否为中文字符函数

    2011-04-07 11:19:00
  • 基于Python OpenCV和 dlib实现眨眼检测

    2021-05-28 18:50:28
  • Anaconda+pycharm安装及环境配置全过程

    2023-05-20 09:40:14
  • SQL Server 2005中的CLR集成

    2009-03-10 15:07:00
  • asp之家 网络编程 m.aspxhome.com