【问题标题】:"error: Unable to find vcvarsall.bat" when compiling Cython code编译 Cython 代码时出现“错误:无法找到 vcvarsall.bat”
【发布时间】:2019-04-09 21:17:53
【问题描述】:

按照here 的建议,我已经成功安装了Microsoft Visual C++ Compiler for Python 2.7 来编译一些 Cython 代码,但是:

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("module1.pyx"))

仍然产生:

错误:无法找到 vcvarsall.bat

如何使用 Python 2.7 编译 Cython 代码(例如在 Windows 7 x64 上)?

注意:我已经仔细阅读了问题error: Unable to find vcvarsall.bat,但主要答案(包括修改msvc9compiler.py)并没有解决。

【问题讨论】:

    标签: python windows visual-c++ cython distutils


    【解决方案1】:

    我在这上面花了几个小时,但在 error: Unable to find vcvarsall.bat 中不容易找到这些信息,这就是为什么我在此处发布它并带有“回答你自己的问题”功能:

    • 第一步:安装Microsoft Visual C++ Compiler for Python 2.7

    • 备注:您不需要像许多论坛帖子中经常建议的那样修改msvc9compiler.py

    • 第 2 步:只需添加import setuptools,这将有助于 Python 和“Microsoft Visual C++ Compiler for Python 2.7”协同工作。

      import setuptools  # important
      from distutils.core import setup
      from Cython.Build import cythonize
      setup(ext_modules=cythonize("module1.pyx", build_dir="build"),
                                                 script_args=['build'], 
                                                 options={'build':{'build_lib':'.'}})
      

      注意:script_args 参数允许仅使用 python setup.py 运行此 setup.py(即在您最喜欢的编辑器中,如 Sublime Text 中的 CTRL+B)instead of having to pass command-line arguments 像这样:python setup.py build

    有效!

    【讨论】:

    • 分享就是关怀 :)
    • 只添加“import setuptools”使错误消失
    • 该死!这是我在网上找到的唯一可行的答案。浪费了我一个小时! (╯°Д°)╯︵┻━┻谢谢你的回答。
    【解决方案2】:

    Python >= 3.5

    我想对@Basj (https://stackoverflow.com/a/53172602/6596203) 的回答发表评论,但我不能,所以我很抱歉。我只想为使用 Python >= 3.5 而不是 Microsoft Visual C++ Compiler for Python 2.7 的人添加@Basj 答案,您可以在此安装程序 Build Tool for Visual Studio 2019 中仅安装 C++,或者您可以简单地使用 Chocolatey 安装它。

    【讨论】:

    • “Visual Studio 2019 构建工具”链接引导我下载整个 Visual Studio。这是故意的吗?
    • @Idonknow,是的,Build Tool for Visual Studio 的下载链接是整个 Visual Studio,但是在安装过程中,您可以选择安装哪个包。我会推荐 Chocolatey,因为它更容易!
    • 如果您的安装在虚拟环境中怎么办?
    • 好问题,我想我没有答案。我猜你还是要在整个系统上安装 C++ 编译器?!
    • @Yehdhih ANNA 这就是为什么我在上面的回答以及上面对 Idonknow 问题的回复中建议巧克力的原因!
    猜你喜欢
    • 2017-01-16
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多