Python中lxml模块的安装

本文转自:这里,感谢原文作者的分享,亲测有效,希望可以帮到更多的童鞋。如有侵权,请告知。


lxml是Python中与XML及HTML相关功能中最丰富和最容易使用的库。lxml并不是Python自带的包,而是为libxml2和libxslt库的一个Python化的绑定。它与众不同的地方是它兼顾了这些库的速度和功能完整性,以及纯Python API的简洁性,与大家熟知的ElementTree API兼容但比之更优越!但安装lxml却又有点麻烦,因为存在依赖,直接安装的话用easy_install, pip都不能成功,会报gcc错误。下面列出来Windows、Linux下面的安装方法:


Windows系统

先确保Python已经安装好,环境变量也配置好了,相应的的easy_install、pip也安装好了.

1. 执行 pip install virtualenv

[python]  view plain  copy
  1. C:\>pip install virtualenv  
  2. Requirement already satisfied (use --upgrade to upgrade): virtualenv in c:\python27\lib\site-package  
  3. s\virtualenv-12.0.4-py2.7.egg  


2. 从官方网站下载与系统,Python版本匹配的lxml文件

http://pypi.python.org/pypi/lxml/2.3/ 


NOTE:

比如说我的电脑是Python 2.7.4, 64位操作系统,那么我就可以下载

[python]  view plain  copy
  1. lxml-2.3-py2.7-win-amd64.egg (md5)     # Python Egg  
  2. 或  
  3. lxml-2.3.win-amd64-py2.7.exe (md5)     # MS Windows installer  

3. 执行 easy_install lxml-2.3-py2.7-win-amd64.egg

   系统会自动为我们安装lxml需要的依赖。

[python]  view plain  copy
  1. D:\Downloads>easy_install lxml-2.3-py2.7-win-amd64.egg    # 进入该文件所在目录执行该命令  
  2. Processing lxml-2.3-py2.7-win-amd64.egg  
  3. creating c:\python27\lib\site-packages\lxml-2.3-py2.7-win-amd64.egg  
  4. Extracting lxml-2.3-py2.7-win-amd64.egg to c:\python27\lib\site-packages  
  5. Adding lxml 2.3 to easy-install.pth file  
  6.   
  7.   
  8. Installed c:\python27\lib\site-packages\lxml-2.3-py2.7-win-amd64.egg  
  9. Processing dependencies for lxml==2.3  
  10. Finished processing dependencies for lxml==2.3  

NOTE:

1. 可用exe可执行文件,方法更简单直接安装就可以

2. 可用easy_install安装方式,也可以用pip的方式

[python]  view plain  copy
  1. #再执行下,就安装成功了!  
  2. >>> import lxml     
  3. >>>   
3. 如用pip安装,常用命令就是:
  • pip install simplejson                      # 安装Python包
  • pip install --upgrade simplejson          # 升级Python包
  • pip uninstall simplejson                    # 卸载Python包

4. 如用Eclipse+Pydev的开发方式,需要移除旧包,重新加载一次

  • Window --> Preferences --> PyDev --> Interperter-python   # 否则导包的时候会报错

Linux系统

因为lxml依赖的包如下:

libxml2, libxml2-devel, libxlst, libxlst-devel, python-libxml2, python-libxslt

所以安装步骤如下:

第一步: 安装 libxml2

  • sudo apt-get install libxml2 libxml2-dev  
第二步: 安装 libxslt
  • sudo apt-get install libxlst libxslt-dev 
第三步: 安装 python-libxml2 和 python-libxslt
  • sudo apt-get install python-libxml2 python-libxslt
第四步: 安装 lxml
  • sudo easy_install lxml

参考官方文档:

http://codespeak.net/lxml/installation.html

猜你喜欢

转载自blog.csdn.net/gaitiangai/article/details/78543733