WSL/Red Hat下Python2.7运行Scrapy报错,求解决方法
问题
工作中需运行Scrapy,但因Python3安装故障暂无法使用。通过pip install scrapy安装了Scrapy 1.8.3,确认其位于Python2.7模块下。可成功执行scrapy startproject checktests与scrapy genspider example example.com命令,但执行scrapy crawl example时,首先出现queuelib的queue.py文件中的错误:
File "/home/synthwave09/.local/lib/python2.7/site-packages/queuelib/queue.py", line 7, in <module> from contextlib import suppress ImportError: cannot import name suppress
手动修改该文件去除contextlib suppress依赖后,又出现如下错误:
File "/usr/local/lib/python2.7/dist-packages/scrapy/crawler.py", line 88, in crawl yield self.engine.open_spider(self.spider, start_requests) TypeError: super() argument 1 must be type, not classobj
请问操作哪里有误,如何让Scrapy在Python2.7环境正常运行?
解决方案
核心问题是安装的Scrapy依赖版本与Python2.7不兼容:
- Scrapy 1.8.3本身支持Python2.7,但默认安装的queuelib版本已放弃Python2.7支持,引入了仅Python3有的
contextlib.suppress,导致第一个报错。 - 手动修改第三方库文件破坏了依赖逻辑,引发后续
super()类型错误。
正确修复步骤:
- 卸载现有不兼容的包
pip uninstall scrapy queuelib -y - 指定兼容版本重新安装
注:queuelib 1.5.0是最后一个支持Python2.7的版本,与Scrapy 1.8.3完全适配。pip install scrapy==1.8.3 queuelib==1.5.0 - 重新执行爬虫命令
直接运行scrapy crawl example即可正常启动爬虫。
内容的提问来源于stack exchange,提问作者Synthwave09
相关产品推荐
相关产品推荐

