适用于 Python 2.7 的 Microsoft Visual C++ 编译器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26140192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:08:43  来源:igfitidea点击:

Microsoft Visual C++ Compiler for Python 2.7

pythonvisual-c++

提问by HYRY

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

我下载了Microsoft Visual C++ Compiler for Python 2.7并安装,完整路径vcvarsall.bat为:

C:\Users\UserName\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python.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.batfile from VS90COMNTOOLSsetting, because it requires that the name of the folder that contains vcvarsall.batis VC:

安装程序并没有将安装信息写入注册表,从 的源代码来看find_vcvarsall(),似乎vcvarsall.batVS90COMNTOOLS设置中找不到该文件,因为它要求包含的文件夹名称vcvarsall.batVC

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

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

如何在不修改注册表或文件夹名称的情况下使用编译器?

采纳答案by Ryan Fau

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.

将 setuptools 更新到 6.0 或更高版本。在这些版本的setuptools可以自动检测微软的Visual C ++编译器为Python 2.7与vcvarsall.bat

Please reference to:

请参考:

  1. https://pypi.python.org/pypi/setuptools/6.1#id4
  2. https://bitbucket.org/pypa/setuptools/issue/258
  1. https://pypi.python.org/pypi/setuptools/6.1#id4
  2. https://bitbucket.org/pypa/setuptools/issue/258

回答by Daniel Samuels

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\VCthen I added a String Value with the name productdirwith the value set to my path to vcvarsall.bat, which was the same as yours.

最后,我只是添加了一个 Key 来创建,COMPUTER\HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\Setup\VC然后我添加了一个字符串值,其名称productdir设置为我的 vcvarsall.bat 路径,与您的相同。

回答by alfonso

Upgrade your setuptools in the command line:

在命令行中升级您的 setuptools:

pip install --upgrade setuptools

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

6.0 以上的最新版 setuptools 应自动检测 Microsoft Visual C++ Compiler for Python 2.7

回答by Christian Long

Look in the setup.pyfile 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().

查看setup.py您尝试安装的软件包的文件。如果它是一个较旧的包,它可能是导入distutils.core.setup()而不是setuptools.setup().

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

我在 2015 年遇到了这些因素的组合:

  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 buildrather than using pip.

  1. 来自http://aka.ms/vcpython27 的适用于 Python 2.7 的 Microsoft Visual C++ 编译器

  2. 使用的旧包 distutils.core.setup()

  3. 试图做python setup.py build而不是使用pip.

If you use a recent version of pip, it will force (monkeypatch) the package to use setuptools, even if its setup.pycalls 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.

如果您使用最新版本的 pip,它会强制(monkeypatch)包使用 setuptools,即使它setup.py调用 distutils。但是,如果您不使用 pip,而只是python setup.py build使用distutils.core.setup(),则构建过程将使用,它不知道编译器安装位置。



Solution

解决方案

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

步骤 1:打开相应的 Visual C++ 2008 命令提示符

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.

打开“开始”菜单或“开始”屏幕,然后搜索“Visual C++ 2008 32-bit Command Prompt”(如果您的 python 是 32 位)或“Visual C++ 2008 64-bit Command Prompt”(如果您的 python 是 64 位) . 运行。命令提示符应在标题栏中显示 Visual C++ 2008 ...。

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

参考http://bugs.python.org/issue23246

Step 3:Build and install

第 3 步:构建和安装

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

cd到要构建的包python setup.py build,然后运行,然后python setup.py install. 如果要安装到 virtualenv,请在构建之前激活它。

回答by wuliang8910

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

Christian Long 提供了一个切实可行的解决方案。但是如果不想每次都在“Visual C++ 2008 32-bit/64-bit Command”中修改,可以简单的找出“vcvarsall.bat”的位置,即“C:\Users\UserName\AppData \Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat”,然后修改 Python27\Lib\distutils\msvc9compiler.py 中的 find_vcvarsall(version) 函数

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.

简单,丑陋但有用。