Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Visual C++ Compiler for Python 2.7

Tags:

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?

like image 518
HYRY Avatar asked Oct 01 '14 11:10

HYRY


People also ask

Does Microsoft Visual Studio support Python?

Visual Studio is a powerful Python IDE on Windows. Visual Studio provides open-source support for the Python language through the Python Development and Data Science workloads (Visual Studio 2017 and later) and the free Python Tools for Visual Studio extension (Visual Studio 2015 and earlier).

What compiler does Visual Studio use for Python?

The use of Microsoft Visual C++ Compiler for Python 2.7 is recommended (If you don't need to compile for ia64).

Does Visual Studio have a compiler for C?

The Visual Studio build tools include a C compiler that you can use to create everything from basic console programs to full Windows Desktop applications, mobile apps, and more.


2 Answers

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
like image 200
Ryan Fau Avatar answered Sep 28 '22 18:09

Ryan Fau


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.

like image 43
Christian Long Avatar answered Sep 28 '22 17:09

Christian Long