Linux系统:Centos7 64位
安装完scray框架后,在python3下导入scrapy时报错:
[root@localhost Python3.6.3]# python3
Python 3.6.3 (default, Aug 21 2018, 20:41:20)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import scrapy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Python3.6.3/lib/python3.6/site-packages/scrapy/__init__.py", line 27, in <module>
from . import _monkeypatches
File "/usr/local/Python3.6.3/lib/python3.6/site-packages/scrapy/_monkeypatches.py", line 20, in <module>
import twisted.persisted.styles # NOQA
ModuleNotFoundError: No module named 'twisted.persisted'
通过排查:
[root@localhost Python3.6.3]# pip3 --version
pip 18.0 from /usr/local/Python3.6.3/lib/python3.6/site-packages/pip (python 3.6)
[root@localhost Python3.6.3]# python3
Python 3.6.3 (default, Aug 21 2018, 20:41:20)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import twisted
>>> twisted.version
Version('twisted', 15, 2, 1)
发现原因就是:我的Twisted版本是15.2的,比较低。而python3,pip3可能版本较高,需要安装Twisted的版本也要高
所以解决方案就是更新Twisted版本:
方法1:
[root@localhost Python3.6.3]# pip3 install twisted --upgrade
方法2:
重新下载个新版本的 Twisted包,解压。
[root@localhost Python3.6.3]# wget https://pypi.python.org/packages/source/T/Twisted/Twisted-17.1.0.tar.bz2
#解压
[root@localhost Python3.6.3]# tar -xjvf Twisted-17.1.0.tar.bz2
#进入文件夹,安装
cd Twisted-17.1.0
python3 setup.py install
更新完版本后,再重新安装scrapy:
[root@localhost Twisted-17.1.0]# pip3 install scrapy
安装完成后,再在python3导入scrapy就不报错了:
[root@localhost Python3.6.3]# python3
Python 3.6.3 (default, Aug 21 2018, 20:41:20)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import scrapy
>>>