安装Python模块Scrappy时遇metadata-generation-failed及requirements.txt缺失问题
问题背景
使用Python 3.10.4和pip 22.2.2安装Scrappy模块时,反复遇到requirements.txt文件未找到的错误:
- 先执行
pip install -r requirements.txt,报错找不到文件; - 直接执行
pip install scrappy,pip尝试了多个Scrappy的预发布版本(alpha到beta),最终在处理旧版本时触发报错,核心错误为setup.py中尝试读取requirements.txt但文件不存在。
完整错误输出:
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [6 lines of output]
Traceback (most recent call last):
File "", line 2, in
File "", line 34, in
File "C:\Users\mohm7\AppData\Local\Temp\pip-install-au7_wkmw\scrappy_e1f30a064214471db41bbe20fedce6c6\setup.py", line 4, in
with open('requirements.txt') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'requirements.txt'
[end of output]note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed× Encountered error while generating package metadata.
╰─> See above for output.note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
解决方法
方法1:手动修复源码包后安装
Scrappy的旧预发布版本存在打包缺陷,未包含requirements.txt但setup.py依赖它。可以手动修复:
- 下载最新的Scrappy源码包(如
Scrappy-0.3.0.alpha.4.tar.gz); - 解压包到本地目录;
- 在解压后的根目录创建
requirements.txt文件,写入依赖:guessit>=3.4.3 tvdb_api>=3.1.0 - 进入解压目录,执行安装命令:
pip install .
方法2:确认是否拼写错误
如果实际需要的是主流爬虫框架Scrapy(注意和Scrappy的拼写差异),直接执行正确的安装命令:
pip install scrapy
内容的提问来源于stack exchange,提问作者exr897

