安装 Scrapy 时遇到了依赖问题,使用 pip 时又发现自带的基础 py 包同样依赖不全,后一并 Google 解决,记一笔备忘。
首先安装 PIP,即 python 的包管理器。
1
| sudo apt-get install python-pip
|
由于 ubuntu 官方库提供的 pip 不是最新的,因此需要先 upgrade 自己:
1
| sudo pip install --upgrade pip
|
此时如果你直接试图安装 scrapy 是会出错的:
1 2 3 4 5 6 7
| sudo pip install scrapy ... ... ... error: command 'x86_64-linux-gnu-gcc' failed with exit status 1 ---------------------------------------- Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-Cs9KYN/cryptography/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-a5Rs1Z-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-Cs9KYN/cryptography/
|
原因是 scrapy 需要抓取网页因而要处理 https,而处理 https 又依赖加解密算法(即 cryptography 包),而 cryptography 又依赖傅立叶变换的算法以及相应的编译环境。Ubuntu 16.04 默认没有安装 libffi-dev 和 libssl-dev,gcc 也不一定安装,而 scrapy 包又没有将相关软件包记到依赖列表里,因此需要先手动安装:
1
| sudo apt-get install libssl-dev libffi-dev python-dev build-essential libxml2-dev libxslt1-dev
|
然后再安装 scrapy 即可顺利完成。
顺便,由于更新强迫症,一并把 pip 自带的包均更新了一遍。pip 没有 apt 那么好用的 dist-upgrade 命令,需要先手动列出旧包然后逐项更新:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| > pip list --outdated
blinker (1.3) - Latest: 1.4 [sdist] colorama (0.3.2) - Latest: 0.3.7 [wheel] Flask (0.10.1) - Latest: 0.11.1 [wheel] html5lib (0.999) - Latest: 0.999999999 [wheel] Jinja2 (2.7.3) - Latest: 2.8 [wheel] ndg-httpsclient (0.3.2) - Latest: 0.4.2 [sdist] numpy (1.8.2) - Latest: 1.11.2 [sdist] Pillow (2.6.1) - Latest: 3.4.2 [sdist] pyasn1 (0.1.7) - Latest: 0.1.9 [wheel] pyinotify (0.9.4) - Latest: 0.9.6 [sdist] pyOpenSSL (0.13.1) - Latest: 16.2.0 [wheel] pyserial (2.6) - Latest: 3.2.1 [wheel] requests (2.4.3) - Latest: 2.12.3 [wheel] setuptools (5.5.1) - Latest: 30.2.0 [wheel] six (1.8.0) - Latest: 1.10.0 [wheel] spidev (3.0) - Latest: 3.2 [sdist] twython (3.1.2) - Latest: 3.4.0 [sdist] urllib3 (1.9.1) - Latest: 1.19.1 [wheel] Werkzeug (0.9.6) - Latest: 0.11.11 [wheel] wheel (0.24.0) - Latest: 0.29.0 [wheel]
|
这些 Py 包可以直接升级:
1 2 3 4 5 6 7 8 9 10 11 12 13
| sudo pip install --upgrade blinker sudo pip install --upgrade colorama sudo pip install --upgrade Flask sudo pip install --upgrade html5lib sudo pip install --upgrade pyasn1 sudo pip install --upgrade pyinotify sudo pip install --upgrade pyserial sudo pip install --upgrade requests sudo pip install --upgrade setuptools sudo pip install --upgrade six sudo pip install --upgrade twython sudo pip install --upgrade urllib3 sudo pip install --upgrade wheel
|
这些 Py 包需要先安装 libssl-dev、libffi-dev、python-dev 以及 build-essential 以后才能升级(其实都是因为要依赖 cryptography):
1 2 3 4 5 6
| sudo apt-get install libssl-dev libffi-dev python-dev build-essential
sudo pip install --upgrade ndg-httpsclient sudo pip install --upgrade numpy sudo pip install --upgrade pyOpenSSL sudo pip install --upgrade spidev
|
这些 Py 包需要先安装 python-dev 以及 libjpeg8-dev 以后才能升级:
1 2 3
| sudo apt-get install python-dev libjpeg8-dev
sudo pip install --upgrade Pillow
|
解决。
话说 numpy 的编译时间真是长啊……