尝试为 Windows 扩展安装 Python 时出现值错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4676728/
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-18 16:53:41  来源:igfitidea点击:

Value error trying to install Python for Windows extensions

pythoncommand-line

提问by user460847

I have Microsoft Visual Studio 2008 installed already. I downloaded the zip file Python for Windows extensionsand extracted the contents into my Python27 folder. There's now a subfolder called pywin32-214. (Is the 32 part a problem? I'm on a 64-bit system.) Here's a transcript from the command line:

我已经安装了 Microsoft Visual Studio 2008。我下载了用于 Windows 扩展的 zip 文件Python并将内容解压缩到我的 Python27 文件夹中。现在有一个名为 pywin32-214 的子文件夹。(32 部分有问题吗?我在 64 位系统上。)这是来自命令行的抄本:

C:\Python27\pywin32-214>setup.py -q install
Building pywin32 2.7.214.0
Traceback (most recent call last):
  File "C:\Python27\pywin32-214\setup.py", line 2152, in <module>
    ('', ('pywin32.pth',)),
  File "C:\Python27\lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Python27\pywin32-214\setup.py", line 1251, in run
    install.run(self)
  File "C:\Python27\lib\distutils\command\install.py", line 563, in run
    self.run_command('build')
  File "C:\Python27\lib\distutils\cmd.py", line 326, in run_command
    self.distribution.run_command(command)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Python27\pywin32-214\setup.py", line 596, in run
    build.run(self)
  File "C:\Python27\lib\distutils\command\build.py", line 127, in run
    self.run_command(cmd_name)
  File "C:\Python27\lib\distutils\cmd.py", line 326, in run_command
    self.distribution.run_command(command)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Python27\lib\distutils\command\build_ext.py", line 340, in run
    self.build_extensions()
  File "C:\Python27\pywin32-214\setup.py", line 858, in build_extensions
    self.compiler.initialize()
  File "C:\Python27\lib\distutils\msvc9compiler.py", line 383, in initialize
    vc_env = query_vcvarsall(VERSION, plat_spec)
  File "C:\Python27\lib\distutils\msvc9compiler.py", line 299, in query_vcvarsal
l
    raise ValueError(str(list(result.keys())))
ValueError: [u'path']

I don't know what to make of this. Help?

我不知道该怎么办。帮助?

采纳答案by cgohlke

If you have a 64 bit Python installation:

如果您安装了 64 位 Python:

Install "Microsoft Visual Studio 2008 Professional Edition" with the "X64 Compiler and Tools" option enabled.

安装“Microsoft Visual Studio 2008 专业版”并启用“X64 编译器和工具”选项。

Alternatively, download pywin32-214.win-amd64-py2.7.exe from http://sourceforge.net/projects/pywin32/files/pywin32/Build%20214/

或者,从http://sourceforge.net/projects/pywin32/files/pywin32/Build%20214/下载 pywin32-214.win-amd64-py2.7.exe

回答by William

Python for Windows extensions is only supported on

Python for Windows 扩展仅在

32-bit MS Windows (95/98), 32-bit MS Windows (NT/2000/XP), All 32-bit MS Windows (95/98/NT/2000/XP), Win2K, WinXP, WinCE

32 位 MS Windows (95/98)、32 位 MS Windows (NT/2000/XP)、所有 32 位 MS Windows (95/98/NT/2000/XP)、Win2K、WinXP、WinCE

Taken from the sourceforge project page. Seems like you may be out of luck!

取自sourceforge 项目页面。看来你可能不走运了!

EDIT: However... reading thisseems to imply otherwise.

编辑:然而......阅读似乎暗示了其他情况。

回答by Ilya

Another possible reason for this problem to appear is that you have just installed Visual Studio and the command prompt you're using had been hanging around from the time beforethe installation.

出现此问题的另一个可能原因是您刚刚安装了 Visual Studio,并且您使用的命令提示符从安装就一直存在。

This is because MSVC installer sets few environment variables and one of these variables ( VS90COMNTOOLS )has to be set for vcvarsall.batto execute correctly. But each running program in Windows holds its own local copy of environment variables that gets inherited (copied) from parent process upon child start-up. Thus, after child has started, it does not receive alterations performed on the system-level envvars. And the only way of getting updated environment variables is trough spawning a new instance of a child process with parent that has updated version of envvars.

这是因为 MSVC 安装程序设置了很少的环境变量,并且必须为vcvarsall.bat设置这些变量之一 (VS90COMNTOOLS)才能正确执行。但是 Windows 中的每个正在运行的程序都拥有自己的本地环境变量副本,这些副本在子启动时从父进程继承(复制)。因此,在 child 启动后,它不会收到对系统级 envvars 执行的更改。获取更新的环境变量的唯一方法是通过具有更新版本的 envvars 的父进程生成子进程的新实例。

回答by frmdstryr

As stated it's trying to use a 32-bit compiler for 64-bit python. I was able to build successfully by:

如前所述,它正在尝试为 64 位 python 使用 32 位编译器。我能够通过以下方式成功构建:

  1. Finding vcvarsx86_amd64.batin C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64(depends on your setup)
  2. Open a cmd prompt
  3. Run SET VS90COMNTOOLS=%VS120COMNTOOLS%(depends on setup, see https://stackoverflow.com/a/10558328/2362877)
  4. Run vcvarsx86_amd64.bat
  5. Then pip install <package>
  1. 寻找vcvarsx86_amd64.batC:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\x86_amd64(取决于你的设置)
  2. 打开一个 cmd 提示
  3. 运行SET VS90COMNTOOLS=%VS120COMNTOOLS%(取决于设置,参见https://stackoverflow.com/a/10558328/2362877
  4. vcvarsx86_amd64.bat
  5. 然后 pip install <package>

回答by Sanjii

I tried all the other answers and a lot more. Ended up installing python 32-bit, which fixed the issue right away.

我尝试了所有其他答案以及更多答案。最终安装了 python 32 位,立即解决了这个问题。

If this is an option, it's most likely the easiest fix.

如果这是一个选项,它很可能是最简单的解决方法。

回答by mmitchell

If you are fixed on Python 2.7 64-bit (like I am) and too cheap to buy VS2008 Professional, you can also download VS2008 Express and install the Windows 7 SDK to get a compatible 64-bit compiler for free. After that, run the Windows 7 SDK SetEnv.Cmd script and then the setup.py:

如果您固定在 Python 2.7 64 位(和我一样)并且太便宜而无法购买 VS2008 Professional,您还可以下载 VS2008 Express 并安装 Windows 7 SDK 以免费获得兼容的 64 位编译器。之后,运行 Windows 7 SDK SetEnv.Cmd 脚本,然后运行 ​​setup.py:

> "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.Cmd"
> setup.py -q install

The SetEnv.Cmd should set up all the environment variables you need for build. When it runs, there should be a message that says "Targeting Windows 7 x64 Debug". You can also use this command to compile in Release mode:

SetEnv.Cmd 应该设置构建所需的所有环境变量。当它运行时,应该有一条消息说“Targeting Windows 7 x64 Debug”。您也可以使用此命令在 Release 模式下进行编译:

> "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.Cmd" /Release
> setup.py -q install

回答by Matthew Roberts

None of these answers worked for me but I found the solution on the issue tracker:

这些答案都不适合我,但我在问题跟踪器上找到了解决方案:

  1. Open cmd.
  2. cd "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin"
  3. Type vcvars32.bat or vcvars64.bat
  4. In the same prompt and -without closing: cd back to the Python module you're trying to install.
  5. The code should now install.
  1. 打开cmd。
  2. cd "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin"
  3. 输入 vcvars32.bat 或 vcvars64.bat
  4. 在相同的提示和 -without closed: cd 回到您尝试安装的 Python 模块。
  5. 现在应该安装代码。

Source: http://bugs.python.org/issue7511

来源:http: //bugs.python.org/issue7511