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

当前回答

当我尝试在windows 10中安装panda时,也出现了同样的错误。在搜索了几个解决方案后,我最终使用了轮子。

首先,将pip升级到最新版本:

easy_install install -U pip

第二,安装车轮:

pip install wheel

第三,下载软件包的whl文件并安装:

pip install [xxx].whl

到目前为止,我认为wheel是在windows上安装Python包的最佳方式。

其他回答

调用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()

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

也许有人会感兴趣,下面的内容适用于py2exe包。(我有windows 7 64位和可移植python 2.7,Visual Studio 2005 Express以及windows 7和.NET Framework 4的windows SDK)

set VS90COMNTOOLS=%VS80COMNTOOLS%

那么:

python.exe setup.py install

以下步骤为我解决了这个问题,我正尝试使用cython扩展创建设置。

安装Microsoft Visual C++编译器Python 2.7版默认安装位置为@C: \Users\PC user\AppData\Local\Programs\Common\Microsoft\Visual C++对于Python这实际上可能会解决问题,请在继续之前测试一次。如果失败,请检查VC中的位置++对于python,vcvarsall.bat文件位于打开msvc9compiler.py记事本中distutils包的文件。在我的箱子里@C: \Anaconda2\Lib\distutils\msvc9compiler.py find_vcvarsall函数在此文件中,通过打印版本来确定VC的版本论点对于Python 2.7,可能是9.0现在创建一个环境变量VS90COMNTOOLS,指向C: \Users\PC user\AppData\Local\Programs\Common\Microsoft\Visual C++用于Python\9.0\VC\bin出于某种原因,distutils期望vcvarsall.bat文件应位于VCdir中,但对于python工具则为VC++它位于9.0的根目录中。要解决此问题,请从path.join(大致围绕第247行)#productdir=os.path.join(toolsdir,os.pardir,os.pardir,“VC”)productdir=os.path.join(toolsdir,os.pardir,os.pardir)

以上步骤为我解决了问题。

我尝试了以上所有的答案,但没有为我工作。我使用的是Windows 10,并安装了Visual Studio 2010在我的情况下,需要将vcvars64.bat添加到C:\Program Files(x86)\Microsoft Visual Studio 10.0\VC\bin\amd64

下面是vcvars64.bat:

CALL "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64

如果未安装,请安装Microsoft SDK 7.1,然后重新运行pip Install dulwich