Mac安装Python插件时unknown type name 'uint64_t'问题解决

前言

博客搭好后居然一年没写东西,o(╯□╰)o。

最近要做数据采集相关的工作,所以在Mac上搭建Python相关的爬虫环境。在按照《Python3网络爬虫开发实战》这本书介绍安装Python的cchardet等插件时却一直报错,我的系统环境是:

#系统参数
OS: 64bit Mac OS X 10.14.4 18E226
Kernel: x86_64 Darwin 18.5.0
#Python参数
Python: 3.7.3
pip: 19.0.3
#clang编译器参数
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

部分报错内容如下:

 /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/sys/resource.h:214:2: error: unknown type name 'uint64_t'
            uint64_t ri_interrupt_wkups;
            ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
    20 errors generated.
    error: command 'clang' failed with exit status 1

alt

原因分析与解决

根据控制台的报错可以知道是clang在编译cchardet过程中无法识别uint64_t标识符导致编译错误,进而安装失败。

可能原因一: clang版本不对

最开始根据error: command 'clang' failed with exit status 1这个报错,网上搜索得到的解答是clang版本不对,执行xcode-select --install,更新安装一下xcode command tools中自带的clang。

~ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates

不过,我本地的xcode command tools已经是最新版本了,期间我也尝试降级clang等方式都没有解决问题,如果有遇到相同问题的朋友在尝试下面移除/usr/local/include目录的方法如果无效,可以尝试一下这个方法,看看能不能解决问题。

可能原因二:系统更新后HomeBrew使用的/usr/local/include目录中头文件混乱

这个原因是我在 Cythonizing fails because of unknown type name 'uint64_t' 这个github讨论主题中看到的,通过主题中提供的方法解决了我遇到的问题

alt

经过测试,删除/usr/local/include目录后,再安装相关python插件则可以编译成功了。 alt

所以Mac下安装python插件遇到“unknown type name 'uint64_t'”报错的朋友可以尝试一下该方法。

参考链接