尝试在 OS X El Capitan 上安装 PyCrypto 时出现致命错误

Fatal error when trying to install PyCrypto on OS X El Capitan

我正在尝试在 OS X 10.11.3 (El Capitan) 上安装 PyCrypto。我正在使用 Python 3.5.1。我从 https://pypi.python.org/pypi/pycrypto 下载了 gzip 文件并解压了它。然后我 运行 python setup.py build 就像说明中所说的那样,它似乎做了一些事情,然后它产生了这个输出:

/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -fwrapv -Wall -Wstrict-prototypes -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/MD2.c -o build/temp.macosx-10.6-intel-2.7/src/MD2.o
src/MD2.c:30:10: fatal error: 'string.h' file not found
#include <string.h>
         ^
1 error generated.
error: command '/usr/bin/clang' failed with exit status 1

我尝试了 python3 setup.py build 并得到了一些非常相似的输出:

/usr/bin/clang -fno-strict-aliasing -Wsign-compare -Wunreachable-code -fno-common -dynamic -fwrapv -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/Library/Frameworks/Python.framework/Versions/3.5/include/python3.5m -c src/MD2.c -o build/temp.macosx-10.6-intel-3.5/src/MD2.o
src/MD2.c:30:10: fatal error: 'string.h' file not found
#include <string.h>
         ^
1 error generated.
error: command '/usr/bin/clang' failed with exit status 1

我尝试使用谷歌搜索来确定要做什么,但找不到任何有用的信息。如何安装 PyCrypto?

编辑: 我还尝试了其他一些方法,例如 pip install pycryptosudo pip3 install pycrypto,但它们没有用。 @l'L'l 通过做几件 st运行ge 帮助我让它工作,这些复杂的事情我自己从来没有做过。它们在下面的答案中进行了总结。

概览:

您正在尝试的手动构建看起来可能会失败,因为它引用了 OS X 10.6 SDK,您可能没有,并且大部分已经过时。此外,SDK 现在存储在与 10.6 SDK 处于最佳状态时完全不同的位置。

新 SDK 位置:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/

旧SDK位置:

/Developer/SDKs/

Non-existent / 过时的 SDK:

因为在尝试构建 PyCrypto 时它似乎引用了 MacOSX10.6.sdk,所以有几件事需要考虑:

  1. 为什么它引用了过时的 SDK
  2. 它引用的 SDK 在哪里设置
  3. 应该如何解决问题

除非我们仔细审核源代码,否则我们可能无法确切知道错误标志设置的位置,但我们可以尽最大努力利用我们掌握的信息。从报错中我们可以看到,有好几处会弹出10.6 SDK的名称:

/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -fwrapv -Wall -Wstrict-prototypes -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/Library

从源构建:

/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/MD2.c -o build/temp.macosx-10.6-intel-2.7/src/MD2.o
src/MD2.c:30:10: fatal error: 'string.h' file not found
#include <string.h>
                 ^
1 error generated.
error: command '/usr/bin/clang' failed with exit status 1

分析这个我们可以看到 PyCrypto 的 MD2.c 文件正试图使用​​标志 -isysroot /Developer/SDKs/MacOSX10.6.sdk 来构建。可能值得尝试 pip 而不是:

使用 pip 安装:

...
fatal error: 'string.h' file not found #include <string.h>
...

同样的错误;我们可能应该找出 <string.h> header 是否存在于系统中 — 让我们快速测试 C 应用程序以找出答案:

正在测试 C headers:

$ echo "#include <string.h>
#include <stdio.h>
int main() { printf(\"TEST\n\"); return 0; }" > t.c
$ clang t.c -o t
$ ./t
TEST

显然 header 确实存在,因为测试工作正常。这告诉我们问题更可能与 10.6 SDK 直接相关(系统上似乎不存在)。

符号链接(non-existing)10.6 SDK 到 10.11 SDK:

由于我们尚未确定 SDK 的实际设置位置,因此我们将继续尝试创建符号链接,以便任何对旧 10.6 SDK 的引用都链接到最新的 SDK(目前为 10.11):

$ cd /Developer/SDKs
$ sudo ln -s /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk MacOSX10.6.sdk

我们可以通过发出以下命令来验证符号链接:

$ ls -lat
total 8
drwxr-xr-x  3 root  wheel  102 Feb 21 15:54 .
lrwxr-xr-x  1 root  wheel   99 Feb 21 15:54 MacOSX10.6.sdk -> /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk
drwxr-xr-x  3 root  wheel  102 Feb 21 15:52 ..

现在我们已经成功创建了符号链接,让我们再次尝试使用 pip 安装 PyCrypto:

$ sudo pip install pycrypto
Collecting pycrypto
  Downloading pycrypto-2.6.1.tar.gz (446kB)
    100% |████████████████████████████████| 446kB 1.2GB/s 
Installing collected packages: pycrypto
  Running setup.py install for pycrypto ... done
Successfully installed pycrypto-2.6.1

No errors! It looks like our problem is solved! Well, almost...

我们仍然需要弄清楚是什么导致在构建期间设置了错误的 (10.6) SDK。让我们使用 xcrun 工具查看默认设置:

$ xcrun --show-sdk-version
10.11

系统默认 SDK 设置为 10.11,因此一定是 Python、PyCrypto 或其他一些我们可能没有考虑到的异常情况将其错误设置为 10.6。

更新:

进行一些侦察后发现 Python 3 似乎是使用 OS X 10.6 SDK 构建的。此外,它还将 SDK 设置为 10.6 并在整个 Python_Framework 的许多地方设置(过时的)路径。参考文献太多,我就不一一列举了,尽管这里有一个例子:

Python_Framework Folder/Versions/3.5/lib/python3.5/config-3.5m/Makefile:79:CONFIGURE_CFLAGS= -arch i386 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk

我只能假设开发人员试图尽可能地向后兼容,然而,不幸的是,它在这个过程中打破了向前兼容性。


备注:

使用 pip 安装 Python 包可以在很多方面(包管理、更新、卸载等)让生活变得更轻松。例如安装 PyCrypto 应该只是发出命令的问题:

$ sudo pip install pycrypto

如果您有 multiple Python's,您可以使用相应的版本号为 Python 安装:

$ sudo pip3.5 install pycrypto

https://pip.pypa.io/en/stable/installing/

当我 运行 pip install pycrypto 时,我在 OSX El Capitan 上遇到了可能相关的问题。我看到 RuntimeError: autoconf error。我所要做的就是 运行 sudo xcodebuild -license 并在查看许可协议后键入 agree。之后我可以使用 pip 安装 pycrypto。