Python3.9.13+Pip22.2.1安装ffmpeg-python报错求助
解决Python3.9安装ffmpeg-python时的AttributeError问题
问题重现
使用Python 3.9.13 + Pip 22.2.1执行pip3 install ffmpeg-python时触发报错,核心错误为:
AttributeError: 'HTMLParser' object has no attribute 'unescape'
完整报错输出:
$ pip3 install ffmpeg-python Collecting ffmpeg-python Using cached ffmpeg_python-0.2.0-py3-none-any.whl (25 kB) Collecting future Downloading future-0.18.2.tar.gz (829 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 829.2/829.2 kB 5.2 MB/s eta 0:00:00 Preparing metadata (setup.py) ... error error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [12 lines of output] Traceback (most recent call last): File "<string>", line 2, in <module> File "<pip-setuptools-caller>", line 14, in <module> File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 14, in <module> from setuptools.dist import Distribution, Feature File "/usr/lib/python3/dist-packages/setuptools/dist.py", line 24, in <module> from setuptools.depends import Require File "/usr/lib/python3/dist-packages/setuptools/depends.py", line 7, in <module> from .py33compat import Bytecode File "/usr/lib/python3/dist-packages/setuptools/py33compat.py", line 54, in <module> unescape = getattr(html, 'unescape', html_parser.HTMLParser().unescape) AttributeError: 'HTMLParser' object has no attribute 'unescape' [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.
原因分析
Python 3.9中HTMLParser.unescape()方法已被移除,相关功能移至html.unescape()。而系统预装的setuptools版本过旧,仍在调用已废弃的HTMLParser().unescape方法,导致安装ffmpeg-python的依赖包future时触发元数据生成失败。
解决方案
方案1:升级全局setuptools和pip
- 执行升级命令:
sudo pip3 install --upgrade setuptools pip
- 升级完成后重新安装ffmpeg-python:
pip3 install ffmpeg-python
方案2:使用虚拟环境隔离依赖(推荐)
如果不想修改全局Python环境,可通过虚拟环境解决版本冲突:
- 创建Python3.9虚拟环境:
python3.9 -m venv ffmpeg_env
- 激活虚拟环境:
- Linux/macOS:
source ffmpeg_env/bin/activate - Windows(PowerShell):
.\ffmpeg_env\Scripts\Activate.ps1
- Linux/macOS:
- 激活后直接安装ffmpeg-python:
pip install ffmpeg-python
内容的提问来源于stack exchange,提问作者Lee
相关产品推荐
相关产品推荐

