Windows下Python 3.6 + VS2017 + Anaconda 解决Unable to find vcvarsall.bat问题

Python 3.6 + VS2017 + Anaconda 解决Unable to find vcvarsall.bat问题

已经安装了VS2017,需要将项目中的C代码翻译为Python代码,在编译setup代码时python setup.py build,出现了error: Unable to find vcvarsall.bat报错。

  1. 找到VS2017的 vcvarsall.bat 文件,如 C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat

  2. 打开Anaconda 的Lib文件夹下的 distutils 文件夹下的 _msvccompiler.py 文件,如:C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\Lib\distutils\_msvccompiler.py

  3. _find_vcvarsall(plat_spec) 函数内容替换为:

def _find_vcvarsall(plat_spec):
    best_dir = r'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\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
    return r'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat', vcruntime
  • 问题1:为什么安装python扩展模块需要安装Microsoft Visual C++呢?

因为有些与操作系统底层密切相关的Python扩展,由于使用C/C++ 进行代码编写,因此在进行安装时需要进行C/C++ 代码的编译工作,而Windows平台的专用C/C++ 代码编译工具就是Microsoft Visual C++ ,因此Python的模块管理工具(如,pip)默认设置的用来编译C/C++ 代码的工具就是VC。Linux平台上所使用的C/C++ 代码编译工具通常都是gcc,因此不涉及安装VS的问题。

  • 问题2:为什么安装Visual Studio可以解决这个问题?

上面已经说明过了,因为Visual Studio中包含Visual C++,安装了Visual Studio之后也就安装了Visual C++。

  • 问题3:为什么有时候安装Visual Studio最新版本都无法解决这个问题?

因为我们当前大部分使用的是CPython,也就是C语言实现的Python版本,我们在Windows上安装的Python也是经过VC编译过的可执行程序。为了保证扩展模块的兼容性,使用Python的模块管理工具(如,pip)安装C语言实现的外部扩展模块时会默认查找并使用与编译当前Python时所使用的相同内部版本或相互兼容的内部版本的的VC,而VS的内部版本与其所包含的VC的内部版本是一致的,因此安装的VS版本过高或过低都可能会出现问题

Cython fatal error C1083: 无法打开包括文件: “numpy/arrayobject.h”: No such file or directory

setup 内添加 include_dirs=[np.get_include()] ,如:

from distutils.core import setup
from Cython.Build import cythonize
import numpy as np
setup(
    ext_modules=cythonize("dtw.pyx"),
    include_dirs=[np.get_include()]
)

猜你喜欢

转载自www.cnblogs.com/kanjian2016/p/11443424.html