繁体   English   中英

错误:无法找到 vcvarsall.bat

[英]error: Unable to find vcvarsall.bat

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

更新:评论指出这里的说明可能是危险的。 考虑使用 Visual C++ 2008 Express edition 或专门构建的Microsoft Visual C++ Compiler for Python详细信息),而不是使用下面的原始答案。 原始错误消息表示未安装所需版本的 Visual C++。


对于 Windows 安装:

在运行 setup.py 进行包安装时,Python 2.7 会搜索已安装的 Visual Studio 2008。您可以通过在调用setup.py之前在VS90COMNTOOLS环境变量中设置正确路径来诱使 Python 使用较新的 Visual Studio。

根据安装的 Visual Studio 版本执行以下命令:

  • Visual Studio 2010 (VS10): SET VS90COMNTOOLS=%VS100COMNTOOLS%
  • Visual Studio 2012 (VS11): SET VS90COMNTOOLS=%VS110COMNTOOLS%
  • Visual Studio 2013 (VS12): SET VS90COMNTOOLS=%VS120COMNTOOLS%
  • Visual Studio 2015 (VS14): SET VS90COMNTOOLS=%VS140COMNTOOLS%

警告:如下所述,如果您尝试编译 python 模块,则此答案不太可能奏效。

有关详细信息,请参阅在 Windows 上为 Python 2.7 构建 lxml

我找到了解决方案。 我在安装“amara”时遇到了完全相同的问题和错误。 我安装了mingw32,但是需要配置distutils。

  1. 我已经安装了 Python 2.6。
  2. 我将 mingw32 安装到C:\\programs\\mingw\\
  3. 将 mingw32 的 bin 目录添加到您的环境变量中: append c:\\programs\\MinGW\\bin; 路径
  4. 将位于C:\\Python26\\Lib\\distutils\\distutils.cfg distutils.cfg文件编辑(如果不存在则创建)为:

     [build] compiler=mingw32
  5. 现在运行easy_install.exe amara

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

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

如果您想使用 Visual Studio C++ 而不是 mingw 进行编译...

  1. 运行python.exe以显示它是用哪个版本的 VC++ 编译的(示例如下所示)。

    使用编译 Python 时使用的 Visual C++ 编译器的相应版本很重要,因为distilutilsget_build_version可防止混合版本( 根据 Piotr 的警告)。

    • 黄色(顶部)是 Python 2.7,使用 MSC v.1500 (Visual Studio C++ 2008) 编译
    • 红色(底部)是 Python 3.4.1,使用 MSC v.1600 (Visual Studio C++ 2010) 编译

    命令行示例显示使用 MSC v.1500 编译的 Python 2.7 和使用 MSC v.1600 编译的 Python 3.4.1

  2. 使用下表[1]将内部 VC++ 版本与相应的 Visual Studio 版本匹配:

     MSC v.1000 -> Visual C++ 4.x MSC v.1100 -> Visual C++ 5 MSC v.1200 -> Visual C++ 6 MSC v.1300 -> Visual C++ .NET MSC v.1310 -> Visual C++ .NET 2003 MSC v.1400 -> Visual C++ 2005 (8.0) MSC v.1500 -> Visual C++ 2008 (9.0) MSC v.1600 -> Visual C++ 2010 (10.0) MSC v.1700 -> Visual C++ 2012 (11.0) MSC v.1800 -> Visual C++ 2013 (12.0) MSC v.1900 -> Visual C++ 2015 (14.0) MSC v.1910 -> Visual C++ 2017 (15.0) MSC v.1911 -> Visual C++ 2017 (15.3) MSC v.1912 -> Visual C++ 2017 (15.5) MSC v.1913 -> Visual C++ 2017 (15.6) MSC v.1914 -> Visual C++ 2017 (15.7) MSC v.1915 -> Visual C++ 2017 (15.8) MSC v.1916 -> Visual C++ 2017 (15.9)
  3. 从上一步下载并安装相应版本的 Visual Studio C++。
    下面列出了针对特定 VC++ 版本的附加说明。

    Visual Studio C++ 2008 注释

    对于 32 位编译器,请下载Visual Studio C++ 2008 Express Edition

    对于 64 位编译器[2] [3] ,请下载适用于 Windows 7 和 .NET Framework 3.5 SP1 的 Windows SDK

    • 取消选中除Developer Tools >> Visual C++ Compilers之外的所有内容,以节省安装您不需要的 SDK 工具的时间和磁盘空间。

    Visual Studio C++ 2010 注释

    根据微软的说法,如果你安装了 Visual Studio 2010 SP1,它可能已经删除了 VC++ 的编译器和库。
    如果是这种情况,请下载Visual C++ 2010 SP1 编译器更新

    Visual Studio C++ 2015 注释

    如果您不需要 Visual Studio IDE,请下载Visual Studio C++ 2015 Build Tools

    Visual Studio C++ 2017 注释

    如果您不需要 Visual Studio IDE,请下载Visual Studio 2017 的构建工具

    建议:如果您同时安装了 32 位和 64 位 Python,您可能还想使用virtualenv创建单独的 Python 环境,这样您就可以一次使用一个或另一个,而不会弄乱您的路径来选择要使用哪个 Python 版本用。

根据@srodriguex 的说法,您可以跳过手动加载批处理文件(步骤 4-6),而是按照此答案将一些批处理文件复制到 Python 正在搜索的位置。 如果这不起作用,以下是最初对我有用的以下步骤。

  1. 打开一个cmd.exe

  2. 尝试安装需要 C 扩展的东西之前,请运行以下批处理文件以将 VC++ 编译器的环境加载到会话中(即环境变量、编译器的路径等)。

    执行:

    • 32 位编译器:

      注意:32 位 Windows 安装将只有C:\\Program Files\\符合预期

      "C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\Common7\\Tools\\vsvars32.bat"

    • 64 位编译器:

      "C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\Common7\\Tools\\vsvars64.bat"

      注意:是的,本机 64 位编译器位于Program Files (x86) 不要问我为什么。
      此外,如果您想知道vcvars64.batvcvarsx86_amd64.bat之间的vcvars64.bat ,或者更重要的是amd64x86_amd64之间的区别,前者用于本机 64 位编译器工具,后者是 64 位交叉编译器,可以在 32 位 Windows 安装上运行。

    更新:
    如果由于某种原因您收到error: ... was unexpected at this time. 其中...是一些字符系列,那么您需要检查您的路径变量是否没有任何多余的字符,如额外的引号或杂散字符。 如果批处理文件一开始就无法理解它,它将无法更新您的会话路径。

  3. 如果一切顺利,您应该会收到以下消息之一,具体取决于 VC++ 的版本和您运行的命令:

    对于 32 位编译器工具:
    Setting environment for using Microsoft Visual Studio 20xx x86 tools.

    对于 64 位编译器工具:
    Setting environment for using Microsoft Visual Studio 20xx x64 tools.

  4. 现在,通过python setup.py installpip install pkg-name运行pip install pkg-name

  5. 希望并交叉你的手指,行星正确对齐,以便 VC++ 合作。

这是怎么回事? 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++ 编译器

最简单的解决方案是将 Christoph Gohlke 的 Windows 安装程序 (.msi) 用于流行的 Python 包。 他为 Python 2.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 以包含构建工具或从http://rubyinstaller.org/downloads下载 DevKit 并按照http://github.com/oneclick/rubyinstaller/wiki/Development-Kit 上的说明进行操作

您可以在https://stackoverflow.com/a/13445719/284795 上阅读有关 Python 打包的更长的咆哮

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

微软现在提供一个捆绑的编译器和头文件只是为了能够编译Python扩展,在好记的网址:

适用于 Python 2.7 的 Microsoft Visual C++ 编译器

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 扩展模块。 适用于 Python 2.7(又名 VC9)的 Microsoft Visual C++ 编译器可从以下网址获得: http : //aka.ms/vcpython27

此包包含为 Python 2.7 32 位和 64 位构建 C 扩展模块所需的所有工具和头文件(请注意,某些扩展模块需要 3rd 方依赖项,例如未包含的 OpenSSL 或 libxml2)。 也支持使用 Visual C++ 2008 构建的其他 Python 版本,因此“Python 2.7”只是广告——它适用于 2.6 和 3.2。

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

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

我刚刚遇到了同样的问题,所以我会在这里讲述我的故事,希望它可以帮助其他有同样问题的人,并节省我刚刚花费的几个小时:

我在 Windows 7 框中有 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 的所有实例。” (从这里

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

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

python setup.py install -c mingw32

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

  1. 将 mingw32 的 bin 目录添加到环境变量中:将PATH附加到C:\\programs\\mingw\\bin;
  2. 创建的distutils.cfg位于C:\\Python27\\Lib\\distutils\\distutils.cfg包含:

     [build] compiler=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 年遇到了这些因素的组合:

  1. 来自http://aka.ms/vcpython27 的适用于 Python 2.7 的 Microsoft Visual C++ 编译器

  2. 使用distutils.core.setup()的旧包

  3. 尝试执行python setup.py build而不是使用pip

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


解决方案

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

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

第二步:设置环境变量

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

SET DISTUTILS_USE_SDK=1
SET MSSdk=1

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

第 3 步:构建和安装

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

也许有人会感兴趣,以下内容适用于 py2exe 包。 (我有 Windows 7 64 位和便携式 python 2.7,Visual Studio 2005 Express 和 Windows SDK for Windows 7 和 .NET Framework 4)

set VS90COMNTOOLS=%VS80COMNTOOLS%

然后:

python.exe setup.py install

我花了将近 2 天的时间弄清楚如何在我的 python 3.4 64 位版本中解决这个问题:Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64) )] 在 win32 上

解决方案 1,难:(在阅读本文之前,请先阅读下面的解决方案 2)最后,这对我有帮助:

  1. 安装Visual C++ 2010 Express
  2. 安装适用于 Windows 7 的 Microsoft Windows SDK v7.1
  3. 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或其他路径,具体取决于您的安装位置
  4. (这似乎是可选的)为 Windows SDK 7.1安装Microsoft Visual Studio 2010 Service Pack 1Microsoft Visual C++ 2010 Service Pack 1 Compiler Update
  5. 之后我尝试pip install numpy但收到以下错误:

     File "numpy\\core\\setup.py", line 686, in get_mathlib_info raise RuntimeError("Broken toolchain: cannot link a simple C program") RuntimeError: Broken toolchain: cannot link a simple C program

    我改变mfinfoNoneC:\\Python34\\Lib\\distutils\\msvc9compiler.py每本https://stackoverflow.com/a/23099820/4383472

  6. 最后在pip install numpy命令之后,我的 avast 防病毒软件试图干扰安装过程,但我很快禁用了它

花了很长时间 - numpy 编译花了几分钟,我什至以为有错误,但最后一切正常。

解决方案 2,简单:(我知道这种方法已经在一个高投票的答案中提到过,但让我重复一遍,因为它确实更容易)在完成所有这些工作后,我明白对我来说最好的方法就是使用已经来自http://www.lfd.uci.edu/~gohlke/pythonlibs/ 的预编译二进制文件。 我永远需要一些本网站不包含的包(或包的一个版本)的可能性很小。 通过这种方式,安装过程也快得多。 例如,要安装numpy

  1. 从该站点下载numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl (如果您有 Python 3.4 64 位)
  2. 在命令提示符或 powershell 中安装它 pip pip install numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl (或文件的完整路径,取决于命令提示符的打开方式)

当我尝试在我的 python 3.5 上安装 numpy 库时遇到了这个问题。 解决办法是安装VS2015。 我有 VS2008、2012、2013,其中没有一个与 python 3.5 兼容。 显然,较新版本的 python 依赖于较新版本的 VS。

还要确保 C++ 通用工具与 Visual Studio 一起安装。

在此处输入图片说明

我想在 Python 2.7 下的 Windows 10 上运行 pysph 并且没有找到 vcvarsall.bat(来自 distutils)

我的解决方案如下:

为 Python 2.7 安装 Microsoft Visual C++(如@Michael 建议)

在 Windows 10 上,它被安装到(我的用户名是 Andreas):

C:\Users\Andreas\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0

将环境变量VS90COMNTOOLS设置为 Visual C++ for Python 2.7 的安装路径(见上路径)。

如果还是不行,就在模块中修改

C:/Python27/lib/distutils

文件msvc9compiler.py 在其中找到函数find_vcvarsall并进行以下修改。

替换行:

productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")

productdir = os.path.join(toolsdir)

这是 vcvarsall.bat 在我的情况下所在的位置(检查,vcvarsall.bat 在您的安装中)。

我尝试了上述所有答案,发现所有答案都不起作用,这可能是我使用的是 Windows 8 并安装了 Visual Studio 2012。在这种情况下,这就是你要做的。

vcvarsall.bat文件位于: C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC

只需选择文件,然后复制它。

然后进入这个目录: C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\Tools

并粘贴文件。 然后,一切都会好起来的。

您可以从http://go.microsoft.com/?linkid=7729279下载免费的 Visual C++ 2008 Express Edition,它将在安装过程中设置 VS90COMNTOOLS 环境变量,从而使用兼容的编译器进行构建。

正如@PiotrDobrogost 在评论中提到的那样,他对另一个问题的回答详细说明了为什么 Visual C++ 2008 是正确的构建工具,但是随着 Python 的 Windows 构建转移到更新版本的 Visual Studio,这可能会发生变化: 构建 lxml适用于 Windows 上的 Python 2.7

在 Windows 7 x64 上使用Python 3.4.1 时遇到了这个问题,不幸的是我需要的包没有合适的 exe 或我可以使用的轮子。 该系统需要一些“变通方法”,详述如下(以及底部的 TLDR )。

使用上面 Jaxrtech 回答中的信息,我确定我需要 Visual Studio C++ 2010(sys.version return MSC v.1600),所以我从他的回答中的链接安装了 Visual C++ 2010 Express,这是http://go.microsoft .com/?linkid=9709949 我安装了所有更新,但正如您在下面看到的,这是一个错误。 此时只应安装 Express 的原始版本(未更新任何内容)。

vcvarsall.bat 现在已经存在,但是在安装包时出现了一个新错误, query_vcvarsall raise ValueError(str(list(result.keys())))ValueError: [u'path'] 此错误还有其他计算器溢出问题,例如在为 Python 2.7 构建/安装 C 模块时出错

我根据那个答案确定 2010 Express 只安装 32 位编译器。 要获得 64 位(和其他)编译器,您需要安装 Windows 7.1 SDK。 请参阅http://msdn.microsoft.com/en-us/windowsserver/bb980924.aspx

但是,这不会为我安装,并且安装程序返回错误installation failed with return code 5100 我在以下链接中找到了解决方案: http : //support.microsoft.com/kb/2717426 简而言之,如果安装了较新版本的 x86 和 x64 Microsoft Visual C++ 2010 Redistributable,它们与 SDK 安装程序中的版本冲突,需要先卸载。

然后安装了 SDK,但我注意到 vcvars64.bat 仍然不存在于C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\bin及其子文件夹中。 vcvarsall.bat 运行 vcvars64 批处理文件,所以没有它,python 包仍然不会安装(我忘记了此时显示的错误)。

然后我在这里找到了一些说明: http : //www.cryptohaze.com/wiki/index.php/Windows_7_Build_Setup#Download_VS_2010_and_Windows_SDK_7.1按照说明,我已经安装了 Express 和 7.1 SDK,所以安装了 SDK 7.1 SP1,并做了缺少头文件修复。 然后我手动创建了内容为CALL setenv /x64 vcvars64.bat 。 我会将所有这些说明粘贴在这里,以免它们丢失。

第 1 步是下载 Visual Studio Express 2010。

http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express是一个很好的起点。 下载安装程序并运行它 (vc_web.exe)。 您不需要额外下载 SQL 2008。

您还需要用于 64 位编译器的 Windows SDK(当前为 7.1) - 除非您只想进行 32 位构建,但不完全支持...

http://www.microsoft.com/en-us/download/details.aspx?id=8279是下载它的一个很好的起点 - 下载后你会想要运行 winsdk_web.exe!

这里的默认安装就好了。

最后,下载并安装 Windows SDK 7.1 SP1 更新: http : //www.microsoft.com/en-us/download/details.aspx?id=4422

并且,为了修复丢失的头文件,VS2010 SP1。 http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=75568aa6-8107-475d-948a-ef22627e57a5

而且,该死的,修复 VS2010 Express 丢失的批处理文件。 这变得非常荒谬。

在 C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\bin\\amd64 中,使用以下内容创建“vcvars64.bat”(您需要以管理员身份运行):

调用 setenv /x64

我的python包仍然没有安装(不记得错误)。 然后我找到了一些使用特殊 SDK 7.1 命令提示符的说明(复制如下),请参阅: https : //mail.python.org/pipermail/distutils-sig/2012-February/018300.html

别管这个问题。 这里有人注意到菜单上的这个项目:开始->所有程序->Microsoft Windows SDK v7.1 ->Windows SDK 7.1 命令提示符

这将运行一个批处理作业,似乎为编译器设置了一个工作环境。 在该提示中,您可以键入“setup.py build”或“setup.py install”。

我按照说明打开了Windows SDK 7.1 命令提示符,并使用它在python 包上运行easy_install。 最后,成功!


TLDR ;

  1. 安装 Visual Studio Express 2010(最好不要更新可再发行组件或 SQL 服务器)。
  2. 安装 Windows 7.1 SDK
  3. 安装 SDK 7.1 SP1 更新和 VS2010 SP1 头文件修复(这一步可能不需要)。
  4. 手动创建C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\bin\\amd64\\vcvars64.bat内容CALL setenv /x64
  5. 开始->所有程序->Microsoft Windows SDK v7.1 ->Windows SDK 7.1 命令提示符打开特殊的x64命令提示符,然后可以与python/easy_install/pip/etc(包括virtual_envs中的那些)一起使用。

使用此链接下载并安装Visual C++ 2015 Build Tools 它将自动下载visualcppbuildtools_full.exe并安装Visual C++ 14.0,而无需实际安装 Visual Studio。 安装完成后,重试 pip install 就不会再出现错误了。

我已经在以下平台和版本上对其进行了测试:

Python 3.6 on Windows 7 64-bit
Python 3.7 on Windows Server 2016 (64-bit system)
Python 3.8 on Windows 10 64-bit

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

“C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvars32.bat”

这是我用来快速将 .pyx 文件编译为 .pyd 的安装脚本:(注意:它使用了 3rd 方模块send2trash

# 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()

下面的步骤为我解决了这个问题,我试图用 cython 扩展创建设置。

  1. 安装适用于 Python 2.7 的 Microsoft Visual C++ 编译器
  2. 默认安装位置是 @ C:\\Users\\PC-user\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python 这实际上可能会解决问题,在继续之前测试一次。
  3. 如果失败,检查VC++中python vcvarsall.bat文件所在的位置
  4. 在记事本中打开 distutils 包的 msvc9compiler.py 文件。
  5. 在我的盒子里,这是@ C:\\Anaconda2\\Lib\\distutils\\msvc9compiler.py 这个文件中的 find_vcvarsall 函数,通过打印版本参数来确定 VC 的版本。 对于 Python 2.7,它可能是 9.0
  6. 现在创建一个环境变量 VS90COMNTOOLS,指向 C:\\Users\\PC-user\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\bin
  7. 出于某种原因,distutils 期望 vcvarsall.bat 文件位于 VC 目录中,但是 VC++ for python 工具将它放在 9.0 的根目录中。要解决此问题,请从 path.join 中删除“VC”(大约在第 247 行附近)

    #productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC") productdir = os.path.join(toolsdir, os.pardir, os.pardir)

上述步骤为我解决了这个问题。

我尝试了很多解决方案,但只有一个对我有用,即安装 Microsoft Visual Studio 2008 Express C++。

我用 C 编写的 Python 2.7 模块遇到了这个问题(yEnc,MS VS 有其他问题)。 请注意,Python 2.7 是使用 MS VS 2008 版本构建的,而不是 2010!

尽管它是免费的,但很难找到,因为 MS 正在推广 VS 2010。不过,MSDN 官方非常直接的链接仍然有效:查看https://stackoverflow.com/a/15319069/2227298以获取下载链接。

如果你安装了 mingw

pip install --global-option build_ext --global-option --compiler=mingw32 packagename

有效,强制 pip 使用 mingw 编译器而不是 Microsoft 编译器进行构建。 有关详细信息,请参见此处https://github.com/pypa/pip/issues/18 (最后一篇文章)。

位于http://www.microsoft.com/en-us/download/details.aspx?id=44266 的Microsoft Visual C++ Compiler for Python 2.7 不是解决方案吗?

2016 年解决这个问题最简单的方法是安装 Chocolatey,然后安装vcpython27包。 打开 Powershell:

> iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
> choco install python2 -y
> choco install vcpython27 -y

我不知道是否为时已晚,但我发现Microsoft Visual C++ Compiler for Python 2.7读取

如果您需要此编译器包,您将收到的典型错误消息是Unable to find vcvarsall.bat

希望这可以帮助!

可以在以下位置找到distutils (以及一些由setuptools官方支持的 MS VС++ 版本和安装变体的详尽列表以及如何使用它们

https://wiki.python.org/moin/WindowsCompilers

它还指定了哪个官方 Win32 Python 版本需要哪个 VC++ 版本。 请注意,MinGW 不受官方支持(有关详细信息,请参见下文)。

简单来说:

  • 对于每个版本,都有一个开箱即用的特定 Visual Studio 版本。
  • 对于独立 SDK,通常需要从 SDK 提示符(=设置环境变量)运行安装。
    • 对于某些人来说,可以通过一次性调整来取消这一要求,而不会产生不利的副作用
  • setuptools而不是distutils支持一些不寻常的 VC++ 版本。
  • 据说MinGW可以“工作”,但仅适用于 x86。 https://bugs.python.org/issue4709说它的支持问题在于 MinGW 本身,甚至 x86 自 Python 3.5 以来也必然会出现问题。

我遇到了同样的问题,目前已经解决了。

“Google”告诉我需要安装“Microsoft Visual C++ Compiler for Python 2.7”。 我不仅安装了该工具,还安装了 Visual C++ 2008 Redistributable,但没有帮助。 然后我尝试安装 Visual C++ 2008 Express Edition。 问题已经解决了!

只需尝试安装 Visual C++ 2008 Express Edition!

如果您想在没有安装 Visual Studio 的 Windows 机器上安装 pyodbc,另一种选择是使用二进制分发版手动安装 pyodbc。

如果您在正在使用的机器上没有管理员权限并且正在尝试设置virtualenv ,这将特别有用。

脚步:

  1. 这里下载最新的 Windows 安装程序 (pyodbc-XXXwin-Y-py2.7.exe)
  2. 使用 7-Zip(或 WinRAR 或其他)打开安装程序可执行文件
  3. 解压pyodbc.pyd和pyodbc-XXX-py2.7.egg-info,放在[python installation directory or virtualenv]\\Lib\\site-packages
  4. 没有第 4 步 :)

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

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

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

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

  1. 将 mingw32 的 bin 目录添加到您的环境变量中
  2. 追加C:\\Programs\\MinGW\\bin;C:\\Programs\\MinGW\\msys\\1.0\\bin; 到路径
  3. 编辑(如果不存在则创建)位于C:\\Python26\\Lib\\distutils\\distutils.cfgdistutils.cfg文件为:

     [build] compiler=mingw32

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

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

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

您可以使用 easy_install 而不是 pip 它对我有用。

尽管有可用的C编译器,但我遇到了同样的错误(我发现这很愚蠢,并且对错误消息没有任何帮助),并且继续出现问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM