我尝试安装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

当前回答

我没有看到任何使用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

其他回答

看起来它正在寻找VC编译器,所以您可以尝试使用-c mingw32提及编译器类型,因为您有msys

python setup.py install -c mingw32

查看您尝试安装的软件包的setup.py文件。如果它是一个较旧的包,那么它可能正在导入distutils.core.setup()而不是setuptools.setup)。

我(在2015年)结合了这些因素:

来自的Microsoft Visual C++编译器Python 2.7http://aka.ms/vcpython27使用distutils.core.setup()的旧包尝试使用python setup.py构建,而不是使用pip。

如果您使用最新版本的pip,它将强制(monkeypatch)包使用setuptools,即使其setup.py调用distutils。但是,如果您不使用pip,而是只使用python setup.py构建,那么构建过程将使用distutils.core.setup(),它不知道编译器的安装位置。


解决方案

步骤1:打开相应的Visual C++2008命令提示符

打开“开始”菜单或“开始”屏幕,搜索“Visual C++2008 32位命令提示符”(如果您的python是32位)或“Visual C++200864位命令提示符(如果您是64位)”。运行它。命令提示符应显示Visual C++2008。。。在标题栏中。

步骤2:设置环境变量

在刚刚打开的命令提示符中设置这些环境变量。

SET DISTUTILS_USE_SDK=1
SET MSSdk=1

参考http://bugs.python.org/issue23246

步骤3:构建和安装

cd到要构建的包,然后运行pythonsesetup.pybuild,然后运行python setup.pyinstall。如果要安装到virtualenv,请在构建之前激活它。

@monkey给出的答案是正确的答案之一,但不完整。

如果您想使用MinGW,您应该选择C、C++以及MinGW安装过程中建议的其他开发工具,以获得“make.exe”

您还必须在env中将路径设置为make.exe。

要完成他的回答,请执行以下步骤:

将mingw32的bin目录添加到环境变量中追加C:\Programs\MinGW\bin;C: \Programs\MinGW\msys\1.0\bin;到PATH将位于C:\Python26\Lib\distutils\distutils.cfg的distutils.cfg文件编辑(如果不存在则创建)为:[生成]编译器=mingw32

确保通过打开新的cmd.exe来设置环境变量。

我也遇到了同样的错误(我觉得这很愚蠢,而且对错误消息没有任何帮助),尽管有一个C编译器可用,我还是继续遇到问题。

令人惊讶的是,最终对我有用的只是将pip和setuptools升级到最新版本。希望这对其他人有所帮助。

我没有看到任何使用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