21

I downloaded Microsoft Visual C++ Compiler for Python 2.7 , and install it, the full path of vcvarsall.bat is:

C:\Users\UserName\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat

But the following code can't return the path of it:

from distutils import msvc9compiler
msvc9compiler.find_vcvarsall(9.0)

The installer doesn't write the install information to the registry, and from the source code of find_vcvarsall(), it seems that it can't find the vcvarsall.bat file from VS90COMNTOOLS setting, because it requires that the name of the folder that contains vcvarsall.bat is VC:

productdir = os.path.join(toolsdir, os.pardir, os.pardir, "VC")

How can I use the compiler without modify registry or folder name?

HYRY
  • 94,853
  • 25
  • 187
  • 187

5 Answers5

25

Update setuptools to 6.0 or greater. In those version setuptools can autodetect Microsoft Visual C++ Compiler for Python 2.7 with the vcvarsall.bat.

Please reference to:

  1. https://pypi.python.org/pypi/setuptools/6.1#id4
  2. https://bitbucket.org/pypa/setuptools/issue/258
Ryan Fau
  • 1,096
  • 9
  • 12
  • This was it for me. I was using virtual env, and it installed a old version of setup tools by default. – tourdownunder Nov 11 '14 at 03:43
  • Well even though you have the last version of setuptools installed, as long as your setup.py uses disutils.core.setup, it won't detect the compiler. At least this was the case for me. – Karavana May 02 '17 at 22:46
22

Look in the setup.py file of the package you are trying to install. If it is an older package it may be importing distutils.core.setup() rather than setuptools.setup().

I ran in to this (in 2015) with a combination of these factors:

  1. The Microsoft Visual C++ Compiler for Python 2.7 from http://aka.ms/vcpython27

  2. An older package that uses distutils.core.setup()

  3. Trying to do python setup.py build rather than using pip.

If you use a recent version of pip, it will force (monkeypatch) the package to use setuptools, even if its setup.py calls for distutils. However, if you are not using pip, and instead are just doing python setup.py build, the build process will use distutils.core.setup(), which does not know about the compiler install location.


Solution

Step 1: Open the appropriate Visual C++ 2008 Command Prompt

Open the Start menu or Start screen, and search for "Visual C++ 2008 32-bit Command Prompt" (if your python is 32-bit) or "Visual C++ 2008 64-bit Command Prompt" (if your python is 64-bit). Run it. The command prompt should say Visual C++ 2008 ... in the title bar.

Step 2: Set environment variables

Set these environment variables in the command prompt you just opened.

SET DISTUTILS_USE_SDK=1
SET MSSdk=1

Reference http://bugs.python.org/issue23246

Step 3: Build and install

cd to the package you want to build, and run python setup.py build, then python setup.py install. If you want to install in to a virtualenv, activate it before you build.

Christian Long
  • 10,385
  • 6
  • 60
  • 58
  • 2
    I have a python package that uses numpy distutils for compiling fortran extensions, so I can't use setuptools hence it can't find visual studio. This solves the problem perfectly. – pkerichang Jan 03 '16 at 20:22
  • 1
    changing the import to setuptools worked like a magic, gracias! I had tones of different c compilers installed but this thing was not able to detect them apparently. Now it can... – Karavana May 02 '17 at 22:40
9

Christian Long provides a practicable solution. But if you do not want to modify it in "Visual C++ 2008 32-bit/64-bit Command" every time, you can simply find out the location of "vcvarsall.bat", i.e. "C:\Users\UserName\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat", and then modify find_vcvarsall(version) function in Python27\Lib\distutils\msvc9compiler.py

like this:

def find_vcvarsall(version):
    productdir= "C:/Users/UserName/AppData/Local/Programs/Common/Microsoft/Visual C++ for Python/9.0"
    vcvarsall = os.path.join(productdir, "vcvarsall.bat")
    if os.path.isfile(vcvarsall):
        return vcvarsall
    else:
        return None

Simple, ugly but useful.

Note that Microsoft no longer lets you download the required Visual Studio 2008 VC++ Build Tools, but the file can be found in the archive: https://web.archive.org/web/20210106040224/https://download.microsoft.com/download/7/9/6/796EF2E4-801B-4FC4-AB28-B59FBF6D907B/VCForPython27.msi

VoteCoffee
  • 4,692
  • 1
  • 41
  • 44
wuliang8910
  • 271
  • 3
  • 4
5

I was having the same issue with this package, it seems that they didn't really test it with a clean installation.

In the end I just added a Key to create COMPUTER\HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\Setup\VC then I added a String Value with the name productdir with the value set to my path to vcvarsall.bat, which was the same as yours.

Daniel Samuels
  • 423
  • 7
  • 25
4

Upgrade your setuptools in the command line:

pip install --upgrade setuptools

The latest versions of setuptools above 6.0 should autodetect the Microsoft Visual C++ Compiler for Python 2.7

alfonso
  • 884
  • 17
  • 31