1. 程式人生 > >windows下,安裝python擴充套件時報錯:unable to find vcvarsall.bat

windows下,安裝python擴充套件時報錯:unable to find vcvarsall.bat

我在win10環境下安裝一個python擴充套件時,報錯如下:

 

錯誤原因:

%PythonInstallPath%\Lib\distutils\msvc9compiler.py 這個檔案是Python用來處理與VC編譯器有關的操作的。

其中該檔案裡的219行find_vcvarsall(version)函式中找不到vcvarsall.bat檔案(報錯資訊就是這裡出現的):說明python 編譯器找不到計算機上面的 VC 編譯器。

def find_vcvarsall(version):
    """Find the vcvarsall.bat file

    At first it tries to find the productdir of VS 2008 in the registry. If
    that fails it falls back to the VS90COMNTOOLS env var.
    """
    vsbase = VS_BASE % version
    try:
        productdir = Reg.get_value(r"%s\Setup\VC" % vsbase,
                                   "productdir")
    except KeyError:
        log.debug("Unable to find productdir in registry")
        productdir = None

    if not productdir or not os.path.isdir(productdir):
        toolskey = "VS%0.f0COMNTOOLS" % version
        toolsdir = os.environ.get(toolskey, None)

        if toolsdir and os.path.isdir(toolsdir):
            productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")
            productdir = os.path.abspath(productdir)
            if not os.path.isdir(productdir):
                log.debug("%s is not a valid directory" % productdir)
                return None
        else:
            log.debug("Env var %s is not set or invalid" % toolskey)
    if not productdir:
        log.debug("No productdir found")
        return None
    vcvarsall = os.path.join(productdir, "vcvarsall.bat")
    if os.path.isfile(vcvarsall):
        return vcvarsall
    log.debug("Unable to find vcvarsall.bat")
    return None

 

那麼為什麼安裝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的問題。

VC版本問題:(下面程式碼是該檔案中的一個函式,是用來確定VC版本的:)

def get_build_version():
    """Return the version of MSVC that was used to build Python.

    For Python 2.3 and up, the version number is included in
    sys.version.  For earlier versions, assume the compiler is MSVC 6.
    """
    prefix = "MSC v."
    i = sys.version.find(prefix)
    if i == -1:
        return 6
    i = i + len(prefix)
    s, rest = sys.version[i:].split(" ", 1)
    majorVersion = int(s[:-2]) - 6
    if majorVersion >= 13:
        # v13 was skipped and should be v14
        majorVersion += 1
    minorVersion = int(s[2:3]) / 10.0
    # I don't think paths are affected by minor version in version 6
    if majorVersion == 6:
        minorVersion = 0
    if majorVersion >= 6:
        return majorVersion + minorVersion
    # else we don't know what version of the compiler this is
    return None
  • 1)如果當前使用的是Python 2.7,可以安裝Visual Studio 2008,也可以安裝 Microsoft Visual C++ Compiler for Python 2.7來解決這個問題;
  • 2)如果當前當前使用的Python 3.x,只能通過安裝相應版本的Visual Studio或Visual C++來解決這個問題。

下載地址:https://download.csdn.net/download/ejiao1233/10686973

 

參考文章連結:

Windows下安裝Python擴充套件模組提示“Unable to find vcvarsall.bat”的問題