无法在 virtualenv 中安装 libxml2

cannot Install libxml2 in virtualenv

我遇到了 libxml2 python 模块的问题。

我正在尝试使用以下命令在 python3 virtualenv 上安装它:

pip install libxml2-python3

但它显示以下错误:

Collecting libxml2-python3   Using cached
https://files.pythonhosted.org/packages/41/97/a2ecf6c5b291799dbd40b3d041d89b0ecdb1b43c8c2503e27991325851cd/libxml2-python3-2.9.5.tar.gz
     Complete output from command python setup.py egg_info:
     failed to find headers for libxml2: update includes_dir

     ---------------------------------------- 
 Command "python setup.py egg_info" failed with error code 1 in
 /tmp/pip-install-72u9ke0y/libxml2-python3/

我尝试了以下链接中提供的解决方案,但没有任何效果:

https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3884

"pip install unroll": "python setup.py egg_info" failed with error code 1

How to install libxml2 in virtualenv?

我在 Fedora 27 上工作os

如果我没记错的话,您需要 libxml2 开发文件。 尝试 运行宁

yum install libxml2-devel

然后 运行 再次安装 pip

它是用 lxml 安装的,实际上你可能想使用 lxml 而不是 libxml2,因为 lxml 基于 libxml2 更 pythonic:

sudo pip install lxml

libxml2 页面说:

Note that some of the Python purist dislike the default set of Python bindings, rather than complaining I suggest they have a look at lxml the more pythonic bindings for libxml2 and libxslt and check the mailing-list.

lxml 页面说:

The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt. It is unique in that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API, mostly compatible but superior to the well-known ElementTree API. The latest release works with all CPython versions from 2.6 to 3.6. See the introduction for more information about background and goals of the lxml project. Some common questions are answered in the FAQ.

是的,我在尝试更新 lmxl pip 包后遇到了同样的错误。问题是 lxml 依赖于 libmxl2libxslt 开发库。因此,您需要确保首先安装了它们。

# pip3 show lxml
Name: lxml
Version: 4.2.5
Summary: Powerful and Pythonic XML processing library combining libxml2/libxslt with the ElementTree API.
Home-page: http://lxml.de/
Author: lxml dev team
Author-email: lxml-dev@lxml.de
License: BSD
Location: /usr/lib/python3.6/site-packages
Requires:
Required-by: Scrapy, parsel, [etc etc]

所以按照以下行安装软件包:

apt-get install libxml2-devel libxslt-devel

(以及它们依次依赖的任何东西。)

我遇到了类似的情况,我的 linux 是 Ubuntu on Windows 10 (WSL - Windows Subsystem for Linux)

我解决这个问题的方法是:

  1. 使用apt,安装:python3 python3-pip, libxml2 libxml2-dev.
me@WSL:~/$ sudo apt-get update
me@WSL:~/$ sudo apt install python3 python3-pip libxml2 libxml2-dev
  1. 使用已安装的 pip,安装 libxml2-python
    me@WSL:~/$ pip3 install libxml2-python3
  1. 完成。 希望这篇笔记对你有帮助。