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

当前回答

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

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

其他回答

关于这个问题的最佳和详尽的答案如下:https://blogs.msdn.microsoft.com/pythonengineering/2016/04/11/unable-to-find-vcvarsall-bat/

在大多数情况下,只要找到适合您所需的python依赖项的.whl包并使用pip安装它就足够了。

在最后一种情况下,您必须安装microsoft编译器并从源代码安装软件包。

我有python 2.73和windows 7。对我有效的解决方案是:

将mingw32的bin目录添加到环境变量:用C:\programs\mingw\bin追加PATH;已创建distutils.cfg,位于C:\Python27\Lib\distutils\distutils.ccfg,包含:[生成]编译器=mingw32

要处理MinGW不再识别-mno cygwin标志的问题,请删除C:\Python27\Lib\distutils\cygwincompiler.py第322到326行中的标志,因此如下所示:

  self.set_executables(compiler='gcc -O -Wall',
                         compiler_so='gcc -mdll -O -Wall',
                         compiler_cxx='g++ -O -Wall',
                         linker_exe='gcc',
                         linker_so='%s %s %s'
                                    % (self.linker_dll, shared_option,
                                       entry_point))

查看您尝试安装的软件包的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,请在构建之前激活它。

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

您可以从安装编译版本http://www.lfd.uci.edu/~gohlke/pythonlibs/