简体   繁体   中英

Python lxml etree on CentOS 6.3

I have Centos 6 on my server and I've been trying to install a few packages (modules) for it. I just brought the ones on my windows to the ftp client in /usr/lib/python2.6/site-packages , but when I run my script one of them - lxml gives me an error:

File "plugins/util/http.py", line 12, in <module>
    from lxml import etree, html
  File "/usr/lib/python2.6/site-packages/lxml/html/__init__.py", line 12, in <module>
ImportError: cannot import name etree

The same code worked perfectly on Windows 7 & Linux Ubuntu 10.04.

Does anyone know why it returns this error? I haven't modified anything, just moved the module from windows to my python2.6 directory on my vps.

@root:

>>> import lxml;print lxml
<module 'lxml' from '/usr/lib/python2.6/site-packages/lxml/__init__.pyc'>
>>>

@ig

gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/libxml2 -I/tmp/pip-build/lxml/src/lxml/includes -I/usr/include/python2.6 -c src/lxml/lxml.etree.c -o build/temp.linux-i686-2.6/src/lxml/lxml.etree.o

unable to execute gcc: No such file or directory

error: command 'gcc' failed with exit status 1

Or install it from EPEL (the easiest way):

# install EPEL if you don't have it yet
sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# instal python-lxml
sudo yum -y install python-lxml

lxml is not a pure Python module. It's mostly written in Cython and hence compiles to native code. The binary files from your Windows machine are incompatible with CentOS (and Linux in general).

Your best bet is to follow the instructions on installing lxml , ie, install the libxslt-devel and libxml2-devel packages and use pip to compile lxml or compile it manually yourself. If you go for compiling it yourself, there's a previous question with some useful info and further info on the lxml site.


Installation

I don't have a CentOS machine to test this on. The easiest method to install would be via ip, which should be available as a package through Yum. If not, you can install it using the following commands (from the documentation for distribute , a pre-requisite for pip):

curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py
easy_install pip

From there, just run:

pip install lxml

And it should install fine.

If you want to skip pip (though, ultimately, pip makes managing dependencies a lot easier, so its well worth doing)

  1. Locate the appropriate version of lxml on PyPI
  2. Download the source from the download URL (should be a tar.gz file)
  3. Extract the tar archive using tar -xzf lxml-<version>.tar.gz
  4. cd into the extracted directory and run python setup.py install

Note that any of the above commands may need to be run as root if your installation is only modifiable by root. Installation commands are python distribute_setup.py , easy_install pip and python setup.py install .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM