venv 中 lxml 的损坏安装无法与 beautifulsoup4 一起使用

Broken installation of lxml in venv cannot be used with beautifulsoup4

我已经创建了一个与tensorflow一起使用的虚拟环境。我已按照 here:

的说明进行操作
virtualenv --system-site-packages -p python3 ./venv

我已经安装了必要的模块,包括 beautiful soup 4 (bs4)lxml using

pip install beautifulsoup4
pip install lxml

这看起来很成功。但是当我试图解析一个 xml 文件时,我收到一个错误,就像我没有安装 lxml:

with open(path/to/xml.file)) as f:
    soup = BeautifulSoup(f, 'xml')

bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: xml. Do you need to install a parser library?

如果我尝试其他解析器按预期工作(或类似的东西):

soup = BeautifulSoup(f, 'html.parser')

在以下情况下似乎工作正常:

soup = BeautifulSoup(f, 'html5lib') 

加载成功但未正确读取 xml。不过这里不是问题。

由于某种原因,我的 lxml 安装似乎损坏了。

Pip 报告它已安装,但我无法卸载或将其降级到其他版本。例如:

pip uninstall lxml

Found existing installation: lxml 3.5.0
Not uninstalling lxml at /usr/lib/python3/dist-packages, outside environment /home/xxxxx/tensorflow_1-15_GPU
Can't uninstall 'lxml'. No files were found to uninstall.

有没有人知道如何强制卸载 lxml 或至少解决问题?

我正在使用 Ubuntu 16.04、python 3.6、beautifulsoup4 4.4.1 和 lxml 3.5.0

编辑:
我最近切换到 python3.6,似乎缺少一些库。尝试降级 lxml 报告(以及其他)错误:

pyconfig.h: No such file or directory

这可能是我模块损坏的原因。 python3.6 没有这样的文件。只有 python2.7 和 python3.5m(虽然我想知道 3.5m 是多少)。

正如我在编辑中提到的,当我尝试降级 lxml:

时(仅)抛出了一条错误消息

pyconfig.h: No such file or directory

这让我检查了 python3.6 的实际库。它们丢失了,在使用以下命令安装它们之后:

sudo apt install python3.6-dev

问题已解决。