编译Cython代码时出现“错误:无法找到vcvarsall.bat”

时间:2018-11-06 13:10:45

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

根据建议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(例如在Windows 7 x64上)编译Cython代码?

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

2 个答案:

答案 0 :(得分:7)

我花了几个小时在此上,并且在error: Unable to find vcvarsall.bat中不容易找到这些信息,这就是为什么我在此处使用“回答自己的问题”功能将其发布的原因:

  • 步骤1:安装Microsoft Visual C++ Compiler for Python 2.7

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

  • 步骤2:只需添加import setuptools,这将有助于Python和“ Python 2.7的Microsoft Visual C ++编译器”一起工作。

    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(即您喜欢的编辑器,如Sublime Text)中的CTRL + B来运行setup.py,instead of having to pass command-line arguments像这样:{{1 }}。

有效!

答案 1 :(得分:5)

Python> = 3.5

我想对@Basj(https://stackoverflow.com/a/53172602/6596203)的答案发表评论,但我不能,我为此表示歉意。我只想为使用Python> = 3.5的用户添加到@Basj答案,而不是Microsoft Visual C ++编译器Python 2.7,您可以在此安装程序Build Tool for Visual Studio 2019中仅安装C ++,也可以简单地使用{{ 3}}。