Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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.

like image 339
Basj Avatar asked Nov 06 '18 13:11

Basj


People also ask

Can not find Vcvarsall bat?

When you see “unable to find vcvarsall. bat”, it means you're installing a package that has an extension module, but only the source code. “vcvarsall. bat” is part of the compiler in Visual Studio that is necessary to compile the module.

What compiler does Cython use?

Unlike most Python software, Cython requires a C compiler to be present on the system. The details of getting a C compiler varies according to the system used: Linux The GNU C Compiler (gcc) is usually present, or easily available through the package system.


2 Answers

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!

like image 120
Basj Avatar answered Sep 17 '22 09:09

Basj


Python >= 3.5

I wanted to comment on @Basj (https://stackoverflow.com/a/53172602/6596203) answer but I couldn't so I apologize. I just want to add to @Basj answer for people using Python >= 3.5, instead of Microsoft Visual C++ Compiler for Python 2.7, you can install just the C++ in this installer Build Tool for Visual Studio 2019 or you can simply install that with Chocolatey.

like image 39
KV88 Avatar answered Sep 18 '22 09:09

KV88