18

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.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • @NewEnglandcottontail: doesn't `python -m pip install --upgrade setuptools` solve your question? also what's the *VStudio* version you have installed? – CristiFati Sep 27 '22 at 07:39

3 Answers3

32

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!

Basj
  • 41,386
  • 99
  • 383
  • 673
8

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.

ZF007
  • 3,708
  • 8
  • 29
  • 48
KV88
  • 354
  • 4
  • 7
  • 2
    The link for 'Build Tool for Visual Studio 2019' leads me to download the whole Visual Studio. Is this intended? – Idonknow Jun 20 '20 at 02:57
  • @Idonknow, yes, the download link for Build Tool for Visual Studio is the whole Visual Studio but during the installation process, you can choose which package to install. I would suggest Chocolatey as it is much easier! – KV88 Jun 20 '20 at 05:51
  • what if your installation is within a virtual enviroment? – Jürgen K. Oct 30 '20 at 08:33
  • Good question, I don't think i have the answer for that. I guess you still have to install the C++ compiler on the whole system?! – KV88 Oct 31 '20 at 14:36
  • the installer of visual studio, asks for 6.46GB to install the C++ build tools !!! – Yehdhih ANNA Jan 12 '21 at 23:23
  • 2
    @Yehdhih ANNA that is why I suggest the chocolatey as in my answer above as well as my reply to Idonknow's question above! – KV88 Jan 13 '21 at 17:51
0

The solution that fixed it for me. Use Git Bash and use the following:

INCLUDE="C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/ucrt/;C:/Program Files (x86)/Windows Kits/10/Include/10.0.22621.0/shared/" \
> LIB="C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22621.0/ucrt/x64;C:/Program Files (x86)/Windows Kits/10/Lib/10.0.22621.0/um/x64" \
> PATH=$PATH:/c/Program\ Files\ \(x86\)/Windows\ Kits/10/bin/10.0.22621.0/x64 \
> python -m pip install <package>

Then run python setup.py build_ext --inplace

Git Bash will detect visual Studio Cmd, Anaconda do not. I am running Microsoft Visual Studio 2022, Microsoft Visual Studio Tools and Microsoft Visual 2017 v 14.00

Theoretical solutions I tried that didn't help me but others said it work below:

The same reason this issue is present these days is because Microsoft made it. Likely a pathing issue and issue is reappearing which is Microsoft specialty since 2018.

https://github.com/pypa/setuptools/issues/3329

This might fix for you but again this is Microsoft:

import setuptools  # as requested
import distutils._msvccompiler

You can also change/check regedit:

[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.2\Setup\VC]
"ProductDir"="C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Auxiliary\\Build"

[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0\Setup\VC]
"ProductDir"="C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC\\Auxiliary\\Build"

Another option is to check the correct environment variables as well as delete any and all visual Studio stuff you do not need/use.

environment variable path and delete old ones and restart pc afterwards:

    C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64_arm
    C:\Users\AAAAA\AppData\Local\Programs\Microsoft VS Code\bin
    C:\Program Files (x86)\Windows Kits\10\bin\10.0.22621.0\x64

Use Anaconda and use the following:

    SET DISTUTILS_USE_SDK=1 & "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" x64

https://learn.microsoft.com/en-us/answers/questions/419525/microsoft-visual-c-140-or-greater-is-required.html
Cave Johnson123
  • 611
  • 1
  • 5
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/248425/discussion-on-answer-by-cave-johnson123-error-unable-to-find-vcvarsall-bat-wh). – Samuel Liew Sep 29 '22 at 01:27