2011-01-04 29 views
61

durante la ejecución de un script en Python, tengo este errorInstalación módulo lxml en Python

from lxml import etree 
ImportError: No module named lxml 

ahora he intentado instalar lxml

sudo easy_install lmxl 

pero me da el siguiente error

Building lxml version 2.3.beta1. 
NOTE: Trying to build without Cython, pre-generated 'src/lxml/lxml.etree.c' needs to be available. 
ERROR: /bin/sh: xslt-config: not found 

** make sure the development packages of libxml2 and libxslt are installed ** 

Usando la configuración de compilación de libxslt

src/lxml/lxml.etree.c:4: fatal error: Python.h: No such file or directory 
compilation terminated. 
error: Setup script exited with error: command 'gcc' failed with exit status 1 

Respuesta

48

Necesita instalar los archivos de encabezado de Python (paquete python-dev en debian/ubuntu) para compilar lxml. Así como libxml2, libxslt, libxml2-dev, y libxslt-dev:

apt-get install python-dev libxml2 libxml2-dev libxslt-dev 
+2

y libxml2-dev y libxslt-dev. –

+1

** libxslt ** no parece ser necesario (no fue para mí, al menos). Por lo tanto, 'apt-get install python-dev libxml2 libxml2-dev libxslt-dev' debería ser suficiente – Matt

+0

Tuve que hacer esto para instalar WeasyPrint, sus instrucciones' apt-get' solo para Debian 7 no funcionaron. – cnst

91

Just Do:

sudo apt-get install python-lxml 

Si usted está planeando instalar desde el código fuente, entonces albertov's answer ayudará. Pero a menos que haya una razón, no lo haga, simplemente instálelo desde el repositorio.

+0

¿Qué quiere decir "solo hacer"? – NewNameStat

+1

Información antigua ahora, pero "solo hacer" significa "no se requerirá nada más, esto es suficiente, creo que todas las otras instrucciones en las respuestas a esta pregunta son superfluas" – GreenAsJade

+0

No estoy seguro de que esto solo haya sucedido en mi servidor Debian 7 y no mi servidor CentOS 6? ¿Nadie sabe? ¿Diferentes paquetes de Python instalados por defecto? – fuzzyanalysis

36

Lo resuelto actualizar la versión con lxml:

pip install --upgrade lxml 
11

Si está ejecutando python3, usted tiene que hacer:

pip3 install lxml

3

para RHEL/CentOS, ejecute " python --version "comando para averiguar la versión de Python. P.ej. a continuación:

$ python --version 
    Python 2.7.12 

Ahora ejecuta "sudo yum búsqueda lxml" para averiguar pitón * Paquete de -lxml.

$ sudo yum search lxml 
Failed to set locale, defaulting to C 
Loaded plugins: priorities, update-motd, upgrade-helper 
1014 packages excluded due to repository priority protections 
============================================================================================================= N/S matched: lxml ============================================================================================================= 
python26-lxml-docs.noarch : Documentation for python-lxml 
python27-lxml-docs.noarch : Documentation for python-lxml 
python26-lxml.x86_64 : ElementTree-like Python bindings for libxml2 and libxslt 
python27-lxml.x86_64 : ElementTree-like Python bindings for libxml2 and libxslt 

Ahora se puede elegir el paquete según su versión de Python y ejecutar comandos, como a continuación:

$ sudo yum install python27-lxml.x86_64 
+0

¡Gracias, funcionó para mí! – Amit

Cuestiones relacionadas