Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

python - "error: Unable to find vcvarsall.bat" when compiling Cython code

As suggested here, I have succesfully installed Microsoft Visual C++ Compiler for Python 2.7 to compile some Cython code, but:

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

still produces:

error: Unable to find vcvarsall.bat

How to compile Cython code with Python 2.7 (for example on Windows 7 x64)?

Note: I already carefully read the question error: Unable to find vcvarsall.bat but the main answers (including modifying msvc9compiler.py) didn't solve it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I spent hours on this, and the information was not easily findable in error: Unable to find vcvarsall.bat, that's why I post it here with the "answer your own question" feature:

  • Step 1: Install Microsoft Visual C++ Compiler for Python 2.7

  • Remark: You don't need to modify msvc9compiler.py as often suggested in many forum posts

  • Step 2: Just add import setuptools that will help Python and "Microsoft Visual C++ Compiler for Python 2.7" work together.

    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':'.'}})
    

    Note: the script_args parameter allows to run this setup.py with just python setup.py (i.e. CTRL+B in your favorite editor like Sublime Text) instead of having to pass command-line arguments like this: python setup.py build.

It works!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...