Ubuntu下WeasyPrint安装报错求助:依赖版本不兼容问题
问题描述
我尝试在Ubuntu中使用命令 pip install weasyprint 安装WeasyPrint,但出现以下依赖冲突错误:
requests 2.18.4 has requirement chardet<3.1.0,>=3.0.2, but you'll have chardet 2.3.0 which is incompatible. tensorboard 1.8.0 has requirement html5lib==0.9999999, but you'll have html5lib 1.0.1 which is incompatible. bleach 1.5.0 has requirement html5lib!=0.9999,!=0.99999, <0.99999999,>=0.999, but you'll have html5lib 1.0.1 which is incompatible.
请问有谁知道如何解决这个问题?
解决方法
这是典型的Python依赖版本冲突问题——全局环境里已安装的包和WeasyPrint(或其他已装工具)的依赖要求不兼容。这里有几个稳妥的解决思路:
1. 使用虚拟环境(最推荐)
虚拟环境可以完全隔离项目的依赖,不会影响全局Python环境里的其他包,是处理这类冲突的标准方案:
- 先安装virtualenv工具:
sudo apt install virtualenv - 为WeasyPrint创建一个专属虚拟环境:
virtualenv weasyprint-venv - 激活虚拟环境:
source weasyprint-venv/bin/activate - 现在在激活的环境里安装WeasyPrint,所有依赖都会独立安装,不会和全局包冲突:
pip install weasyprint
以后使用WeasyPrint时,只要先激活这个虚拟环境即可。
2. 升级冲突的依赖包
如果你不想用虚拟环境,可以尝试升级冲突的包到兼容版本:
- 首先升级chardet到符合requests要求的版本(3.0.2~3.1.0之间):
pip install --upgrade chardet==3.0.4 - 然后处理html5lib的冲突:tensorboard 1.8.0和bleach 1.5.0要求的html5lib版本太旧,和WeasyPrint需要的1.0.1不兼容。可以尝试升级这两个包到支持html5lib 1.0.1的版本:
pip install --upgrade tensorboard bleach - 完成后再重新安装WeasyPrint:
pip install weasyprint
3. 使用Ubuntu系统包安装WeasyPrint
Ubuntu官方源里已经打包了WeasyPrint,用系统包安装的话,依赖问题会被系统自动处理:
sudo apt install weasyprint
这种方式安装的WeasyPrint可能版本不是最新的,但胜在稳定,适合不需要最新功能的场景。
内容的提问来源于stack exchange,提问作者Pankaj Kalania




