python - 在 Windows 上安装 NumPy

标签 python numpy pip

我只是无法在 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/ .服务器返回一个 HTML 文档,而 pip期待一个存档。所以这是行不通的。

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

  • 来源,正如您当时所尝试的那样
  • 来自预编译的软件包

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

    对于第二种情况,您有不同种类的预编译包:
  • 使用 pip 安装的轮子还有
  • 安装程序,您在 Windows 上用作标准安装程序

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

    一个简单的解决方案

    您可以找到 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++ for 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
  • visual studio - Detecting compiler versions during compile time - Stack Overflow 3
  • Pre-defined Compiler Macros / Wiki / Compilers
  • WinCvt - Windows Converter toolkit

  • 在我上面给出的示例中,这意味着 Microsoft Visual C++ 2010 64 位。

    如何安装 Visual C++

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

    以下是一些引用链接:
  • Download Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1 from Official Microsoft Download Center : 对于 Visual C++ 15.00 (Visual Studio 2008)。对应于 WinSDK 7。
  • Download Microsoft Windows SDK for Windows 7 and .NET Framework 4 from Official Microsoft Download Center :用于 Visual C++ 16.00 (Visual Studio 2010)。对应于 WinSDK 7.1。
  • installation - where can I download the full installer for Visual C++ Express? - Super User
  • Visual Studio & co. downloads

  • 故障排除

    您可能在安装 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 Installation Failure
  • Error installing Windows 7 SDK 7.1 with VS2008, VS2010 Premium on Win 7 32bit

  • 作为解决方案,您可以查看此链接:Windows SDK Fails to Install with Return Code 5100

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

    使用开发工具

    通常你应该运行 vsvarsall.bat (位于 Visual Studio 安装路径的 VC 文件夹内 - 例如: 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 .

    关于python - 在 Windows 上安装 NumPy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28413824/

    相关文章:

    python - NetworkX:边和节点属性的子图同构

    python - Tkinter 标度中的对数标度

    python - 计算过程中 cumprod 的裁剪值

    python pip静默安装

    python : Appending values to existing spreadsheet from updated values

    python - 如何使用python统计网页有多少个页面

    Python,用格式化字符串替换所有数组值

    python - 将值 append 到 3D 数组 numpy

    installation - H5PY安装: error command 'x86_64-linux-gnu-gcc' failed with exit status 1

    python - pip3 停止将可执行文件安装到/usr/local/bin 中