在 Windows 上安装 NumPy

2024-07-04

我根本无法在 Windows 上安装 NumPy。我不断收到此错误 -

PS C:\python27> pip install http://sourceforge.net/projects/numpy/file/NumPy/
Collecting http://sourceforge.net/projects/numpy/files/NumPy/
Downloading http://sourceforge.net/projects/numpy/files/NumPy/ (58kB)
100% |################################| 61kB 15kB/s
Cannot unpack file c:\users\toshiba\appdata\local\temp\pip-qev4rz-unpack\NumPy
(downloaded from c:\users\toshiba\appdata\local\temp\pip-omripn-build, content-type: text/html; charset=utf-8); cannot detect archive format
Cannot determine archive format of c:\users\toshiba\appdata\local\temp\pip-omripn-build

我之前有一个 Python 64 位版本,我不确定 NumPy 版本是否与 64 位 Python 兼容。所以我卸载了它并安装了32位Python版本。但我仍然遇到同样的错误。虽然我的 Python 32 位版本运行良好。

我尝试了“pip install numpy”,但这最后给了我以下错误 -

C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'define_macros'

  warnings.warn(msg)

error: Unable to find vcvarsall.bat

----------------------------------------
Command "C:\Python27\python.exe -c "import setuptools,tokenize;__file__='c:\\users\\toshiba\\appdata\\local\\temp\\pip-build-hdhqex\\numpy\\setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'),__file__, 'exec'))" install --record c:\users\toshiba\appdata\local\temp\pip-x_6llm-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in c:\users\toshiba\appdata\local\temp\pip-build-hdhqex\numpy

我可能做错了什么?


一些解释

在第一种情况下,我没有检查,但我想pip直接下载给定URL对应的资源:http://sourceforge.net/projects/numpy/file/NumPy/ http://sourceforge.net/projects/numpy/file/NumPy/。服务器返回一个 HTML 文档,同时pip期待一份存档。所以这是行不通的。

那么安装Python包基本上有两种方法:

  • 从来源,正如你当时尝试的那样
  • 来自预编译的包

第一种情况,你用命令尝试过pip install numpy,但由于这个包包含本机代码,因此需要正确安装开发工具(我总是发现在 Windows 上这样做很麻烦,但我这样做了,所以它显然是可行的)。你遇到的错误error: Unable to find vcvarsall.bat意味着您没有安装工具,或者没有正确设置环境。

对于第二种情况,您有不同类型的预编译包:

  • 轮子,您安装的pip as well
  • 安装程序,用作 Windows 上的标准安装程序

对于这两者,您需要检查二进制文件是否已针对您的 Python 架构(32 或 64 位)和版本进行了严格编译。

一个简单的解决方案

你可以找到几个轮子numpy: http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy。要获得正确的架构,请检查名称win32对于 32 位和amd64对于 64 位。要获取正确的 Python 版本,请检查cpXX:第一个 X 是主要版本,第二个 X 是次要版本,例如cp27表示 CPython 2.7。

例子:pip install numpy‑1.9.2rc1+mkl‑cp27‑none‑win32.whl

硬解:安装和使用开发工具

免责声明: 以下所有解释可能不太清楚。它们是在不同时刻进行的多次调查的结果,但在我的配置中,它们产生了一个可行的解决方案。有些链接可能无用或多余,但这就是我注意到的。所有这些都需要一些清理,可能还需要概括。

首先,你需要明白disutils- 这是预安装的软件包,它在比pip(后者使用的) - 将尝试使用与用于构建您安装的 Python 机器严格匹配的编译器。

Python 的官方发行版使用 Microsoft Visual C++ 作为 Microsoft Windows 软件包。所以在这种情况下你需要安装这个编译器。

如何找到合适的 Visual C++ 版本

Python 使用此命令打印的字符串python -c "import sys; print(sys.version)"(或者当您调用交互式 shell 时)将如下所示:

3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)]

方括号中的最后一部分是编译器的标识部分。不幸的是,这并不是很简单,并且您那里有通信列表:

  • windows - What version of Visual Studio is Python on my computer compiled with? - Stack Overflow https://stackoverflow.com/questions/2676763/what-version-of-visual-studio-is-python-on-my-computer-compiled-with
    • Visual Studio - 在编译时检测编译器版本 - VoidCC https://stackoverflow.com/questions/3592805/detecting-compiler-versions-during-compile-time3
    • 预定义编译器宏 / Wiki / 编译器 http://sourceforge.net/p/predef/wiki/Compilers/
    • WinCvt - Windows 转换器工具包 http://wincvt.sourceforge.net/

在我上面给出的例子中,这意味着微软 Visual C++ 2010 64 位.

如何安装 Visual C++

您再也找不到现代版本的 Visual C++ 独立包了。因此您需要安装 Windows SDK 本身。

以下是一些参考链接:

  • 从 Microsoft 官方下载中心下载适用于 Windows 7 和 .NET Framework 3.5 SP1 的 Microsoft Windows SDK http://www.microsoft.com/en-us/download/details.aspx?id=3138:适用于 Visual C++ 15.00 (Visual Studio 2008)。对应WinSDK 7。
  • 从 Microsoft 官方下载中心下载适用于 Windows 7 和 .NET Framework 4 的 Microsoft Windows SDK http://www.microsoft.com/en-us/download/details.aspx?id=8279:适用于 Visual C++ 16.00 (Visual Studio 2010)。对应WinSDK 7.1。
  • installation - where can I download the full installer for Visual C++ Express? - Super User https://superuser.com/questions/443617/where-can-i-download-the-full-installer-for-visual-c-express

故障排除

安装SDK时可能会出现错误: DDSet_Error: Patch Hooks: Missing required property 'ProductFamily': Setup cannot continue. DDSet_Warning: Setup failed while calling 'getDLLName'. System error: Cannot create a file when that file already exists.

他们已经在几个问题中得到了报道:

  • Windows 7 SDK安装失败 https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/8f3350f9-0b47-40ae-b070-f2ccbf041875/windows-7-sdk-installation-failure
  • 在 Win 7 32 位上使用 VS2008、VS2010 Premium 安装 Windows 7 SDK 7.1 时出错 https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/6e6c8a17-1666-42fa-9b5b-dfc21845d2f9/error-installing-windows-7-sdk-71-with-vs2008-vs2010-premium-on-win-7-32bit?forum=windowssdk

作为解决方案,您可以检查此链接:Windows SDK 安装失败并返回代码 5100 https://support.microsoft.com/kb/2717426

问题是删除所有冲突的(理解:SDK 安装程序尝试自行安装的版本)Visual C++ 可再发行版本。

使用开发工具

通常你应该运行vsvarsall.bat(位于内部VCVisual Studio安装路径的文件夹 - 示例:C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat)设置适当的环境变量,以便执行distutils尝试编译包时不会失败。

此批处理脚本接受一个参数,该参数应设置所需的架构。然而,我发现在尝试其中几个参数时,免费版本的 SDK 缺少一些额外的脚本。

只是说,如果您正在编译 32 位架构,只需调用vsvarsall.bat应该管用。如果需要编译64位,可以直接调用SetEnv.cmd,位于 SDK 安装路径下的某个位置 - 例如:"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Windows 上安装 NumPy 的相关文章

随机推荐