我尝试安装Python包dulwich:

pip install dulwich

但我收到了一条神秘的错误消息:

error: Unable to find vcvarsall.bat

如果我尝试手动安装软件包,也会发生同样的情况:

> python setup.py install
running build_ext
building 'dulwich._objects' extension
error: Unable to find vcvarsall.bat

当前回答

您需要安装一个与用于构建Python的编译器兼容的Microsoft编译器。这意味着您需要Visual C++2008(或更高版本,有一些调整)。

微软现在提供了一个捆绑的编译器和头文件,以便能够在令人难忘的URL上编译Python扩展:

Microsoft Visual C++编译器Python 2.7版http://aka.ms/vcpython27

这是一个相对较小的包装;85MB可下载,无需管理员权限即可安装,无需重新启动。这个名字有点误导人,编译器适用于最初使用Visual C++2008编译的任何Python版本,而不仅仅是Python 2.7。

如果启动Python交互式提示或打印sys.version,请查找MSC版本字符串;如果是MSC v.1500,您可以使用此工具。

从最初的公告到distutils列表:

微软发布了Python 2.7的编译器包,使人们更容易在Windows上构建和分发C扩展模块。Microsoft Visual C++编译器Python 2.7版(又名VC9)可从以下网站获得:http://aka.ms/vcpython27 此包包含为Python 2.7 32位和64位构建C扩展模块所需的所有工具和头文件(请注意,某些扩展模块需要第三方依赖项,如未包含的OpenSSL或libxml2)。也支持使用Visual C++2008构建的Python的其他版本,因此“Python 2.7”只是一个广告——它可以在2.6和3.2中正常工作。

请注意,您需要安装setuptools6.0或更高版本(在下载页面的系统要求中列出)。您正在安装的项目必须使用setuptools.setup(),而不是distutils,否则自动检测将无法工作。

微软表示,他们希望保持URL稳定,以便自动化脚本可以轻松引用它。

其他回答

对于Python 3.4,依赖于Visual Studio 2010。安装Visual C++2010 Express为我解决了这个问题。

欺骗它使用VS 2008或2013安装,而我碰巧没有使用。

我也遇到过同样的问题,所以我将在这里讲述我的故事,希望它能帮助其他人解决同样的问题并节省我刚刚花的几个小时:

我在一个windows7盒子里有mingw(g++(GCC)4.6.1)和python 2.7.3,我正在尝试安装PyCrypto。

在运行setup.py install时,这一切都以以下错误开始:

error: Unable to find vcvarsall.bat

通过将mingw指定为所选编译器,在谷歌搜索错误后轻松解决:

setup.py install build --compiler=mingw32

问题是,然后我得到了一个不同的错误:

configure: error: cannot run C compiled programs.

事实证明,我的防病毒软件阻止了新编译的.exe的执行。我刚刚禁用了防病毒“常驻屏蔽”,并转到下一个错误:

cc1.exe: error: unrecognized command line option '-mno-cygwin' 
error: command 'gcc' failed with exit status 1

这解决了这个问题:“要么安装稍微旧一点的MinGW版本,要么在Python目录中编辑distutils\cygwinccompiler.py以删除-mno-cygwin的所有实例。”(从这里开始)

现在,我终于可以开始工作了。

我没有看到任何使用vswhere的答案,我认为这是自Visual Studio 15.2以来正确的方法。

下面是我运行vsvars64.bat的方法(我想这与vsvarsall类似)

def init_vsvars():
    cprint("")
    cprint_header("Initializing vs vars")
    vswhere_path = r"%ProgramFiles(x86)%/Microsoft Visual Studio/Installer/vswhere.exe"
    vswhere_path = path.expandvars(vswhere_path)
    if not path.exists(vswhere_path):
        raise EnvironmentError("vswhere.exe not found at: %s", vswhere_path)

    vs_path = common.run_process(".", vswhere_path,
                                 ["-latest", "-property", "installationPath"])
    vs_path = vs_path.rstrip()

    vsvars_path = os.path.join(vs_path, "VC/Auxiliary/Build/vcvars64.bat")
    # common.run_process(".", vsvars_path, [])
    os.system('"%s"' % vsvars_path)

run_process做了很多事情,但基本上归结为:

    output = ""
    process = subprocess.Popen(
        commandline,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        universal_newlines=True)
    for stdout_line in iter(process.stdout.readline, ""):
        cprint(stdout_line)
        output += stdout_line
    process.stdout.close()

    return_code = process.wait()
    return output

调用importsetuptools将对补丁distutils进行模仿,以强制与Visual Studio兼容。手动调用vcvars32.bat将设置虚拟环境并防止编译器抛出其他常见错误。对于VS 2017,文件位于

“C:\Program Files(x86)\Microsoft VisualStudio\2017\社区\VC\Auxiliary\Build\vcvars32.bat“

以下是我用来将.pyx文件快速编译为.pyd的安装脚本:(注意:它使用第三方模块发送2个

# cython_setup.py
import sys, os, time, platform, subprocess
from setuptools import setup, find_packages
from Cython.Build import cythonize
from traceback import format_exc

# USAGE:
#
#   from cython_setup import run
#   run(pyx_path)

# vcvars = r"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars32.bat"

# NOTE: to use visual studio 2017 you must have setuptools version 34+
vcvars = r"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars32.bat"


def _build_ext():
    try:
        pyx_path = sys.argv.pop(-1)
        pyx_path = os.path.abspath(pyx_path)
        if not os.path.exists(pyx_path):
            raise FileNotFoundError(f"{pyx_path} does not exist")
        project_name = sys.argv.pop(-1)
        os.chdir(os.path.abspath(os.path.dirname(pyx_path)))

        print("cwd: %s" % os.getcwd())
        print(os.path.abspath("build"))
        setup(
            name=project_name,
            # cmdclass = {'build_ext': build_ext},
            packages=find_packages(),
            # ext_modules=cythonize(extensions)
            ext_modules=cythonize(pyx_path,
                                  compiler_directives={'language_level': 3, 'infer_types': True, 'binding': False},
                                  annotate=True),
            # include_dirs = [numpy.get_include()]
            build_dir=os.path.abspath("build")
        )
    except:
        input(format_exc())


def retry(func):
    def wrapper(*args, **kw):
        tries = 0
        while True:
            try:
                return func(*args, **kw)
            except Exception:
                tries += 1
                if tries > 4:
                    raise
                time.sleep(0.4)

    return wrapper


@retry
def cleanup(pyx_path):
    from send2trash import send2trash
    c_file = os.path.splitext(pyx_path)[0] + ".c"
    if os.path.exists(c_file):
        os.remove(c_file)

    if os.path.exists("build"):
        send2trash("build")


def move_pyd_files(pyx_path):
    pyx_dir = os.path.dirname(pyx_path)
    build_dir = os.path.join(pyx_dir, "build")
    if not os.path.exists(build_dir):
        raise RuntimeError(f"build_dir {build_dir} did not exist....")
    found_pyd = False
    for top, dirs, nondirs in os.walk(build_dir):
        for name in nondirs:
            if name.lower().endswith(".pyd") or name.lower().endswith(".so"):
                found_pyd = True
                old_path = os.path.join(top, name)
                new_path = os.path.join(pyx_dir, name)
                if os.path.exists(new_path):
                    print(f"removing {new_path}")
                    os.remove(new_path)
                print(f"file created at {new_path}")
                os.rename(old_path, new_path)
    if not found_pyd:
        raise RuntimeError("Never found .pyd file to move")

def run(pyx_path):
    """
    :param pyx_path:
    :type pyx_path:
    :return: this function creates the batch file, which in turn calls this module, which calls cythonize, once done
    the batch script deletes itself... I'm sure theres a less convoluted way of doing this, but it works
    :rtype:
    """
    try:
        project_name = os.path.splitext(os.path.basename(pyx_path))[0]
        run_script(project_name, os.path.abspath(pyx_path))
    except:
        input(format_exc())


def run_script(project_name, pyx_path):
    dirname = os.path.dirname(pyx_path)
    # ------------------------------
    os.chdir(dirname)
    if os.path.exists(vcvars):
        #  raise RuntimeError(
        # f"Could not find vcvars32.bat at {vcvars}\nis Visual Studio Installed?\nIs setuptools version > 34?")
        subprocess.check_call(f'call "{vcvars}"', shell=True)

    cmd = "python" if platform.system() == "Windows" else "python3"
    subprocess.check_call(f'{cmd} "{__file__}" build_ext "{project_name}" "{pyx_path}"', shell=True)
    move_pyd_files(pyx_path)
    cleanup(pyx_path)


if len(sys.argv) > 2:
    _build_ext()

发生什么事?Python模块可以部分用C或C++编写(通常是为了速度)。如果您尝试用Pip(或setup.py)安装这样的包,它必须从源代码编译C/C++。开箱即用,Pip会厚颜无耻地假设您安装了Microsoft Visual C++编译器。如果您没有它,您将看到这条神秘的错误消息“错误:无法找到vcvarsall.bat”。

规定的解决方案是安装C/C++编译器,或者Microsoft Visual C++,或者MinGW(一个开源项目)。然而,安装和配置两者都非常困难。(编辑2014:微软发布了一个专门针对Python 2.7的C++编译器)

最简单的解决方案是为流行的Python包使用Christoph Gohlke的Windows安装程序(.msi)。他为Python2.x和3.x、32位和64位构建安装程序。您可以从http://www.lfd.uci.edu/~gohlke/pythonlibs/


如果你也认为“错误:找不到vcvarsall.bat”是一条荒谬而晦涩且毫无帮助的消息,那么请在http://bugs.python.org/issue2943将其替换为更有用、更友好的信息。

相比之下,Ruby附带了一个包管理器Gem,并提供了一个准官方的C/C++编译器DevKit。如果您尝试安装不带软件包的软件包,您会看到以下有用的友好信息:

请更新PATH以包含构建工具或从下载DevKithttp://rubyinstaller.org/downloads并遵循以下说明http://github.com/oneclick/rubyinstaller/wiki/Development-Kit

您可以在https://stackoverflow.com/a/13445719/284795