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
- C:\>pip install virtualenv
- Requirement already satisfied (use --upgrade to upgrade): virtualenv in c:\python27\lib\site-package
- s\virtualenv-12.0.4-py2.7.egg
C:\>pip install virtualenv Requirement already satisfied (use --upgrade to upgrade): virtualenv in c:\python27\lib\site-package 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位操作系统,那么我就可以下载
- lxml-2.3-py2.7-win-amd64.egg (md5) # Python Egg
- 或
- lxml-2.3.win-amd64-py2.7.exe (md5) # MS Windows installer
lxml-2.3-py2.7-win-amd64.egg (md5) # Python Egg 或 lxml-2.3.win-amd64-py2.7.exe (md5) # MS Windows installer
3. 执行 easy_install lxml-2.3-py2.7-win-amd64.egg
- D:\Downloads>easy_install lxml-2.3-py2.7-win-amd64.egg # 进入该文件所在目录执行该命令
- Processing lxml-2.3-py2.7-win-amd64.egg
- creating c:\python27\lib\site-packages\lxml-2.3-py2.7-win-amd64.egg
- Extracting lxml-2.3-py2.7-win-amd64.egg to c:\python27\lib\site-packages
- Adding lxml 2.3 to easy-install.pth file
- Installed c:\python27\lib\site-packages\lxml-2.3-py2.7-win-amd64.egg
- Processing dependencies for lxml==2.3
- Finished processing dependencies for lxml==2.3
D:\Downloads>easy_install lxml-2.3-py2.7-win-amd64.egg # 进入该文件所在目录执行该命令 Processing lxml-2.3-py2.7-win-amd64.egg creating c:\python27\lib\site-packages\lxml-2.3-py2.7-win-amd64.egg Extracting lxml-2.3-py2.7-win-amd64.egg to c:\python27\lib\site-packages Adding lxml 2.3 to easy-install.pth file Installed c:\python27\lib\site-packages\lxml-2.3-py2.7-win-amd64.egg Processing dependencies for lxml==2.3 Finished processing dependencies for lxml==2.3
NOTE:
2. 可用easy_install安装方式,也可以用pip的方式
- #再执行下,就安装成功了!
- >>> import lxml
- >>>
#再执行下,就安装成功了! >>> import lxml >>>3. 如用pip安装,常用命令就是:
- pip install simplejson # 安装Python包
- pip install --upgrade simplejson # 升级Python包
- pip uninstall simplejson # 卸载Python包
4. 如用Eclipse+Pydev的开发方式,需要移除旧包,重新加载一次
- Window --> Preferences --> PyDev --> Interperter-python # 否则导包的时候会报错
因为lxml依赖的包如下:
libxml2, libxml2-devel, libxlst, libxlst-devel, python-libxml2, python-libxslt
所以安装步骤如下:
第一步: 安装 libxml2
- $ sudo apt-get install libxml2 libxml2-dev
- $ sudo apt-get install libxlst libxslt-dev
- $ sudo apt-get install python-libxml2 python-libxslt
- $ sudo easy_install lxml
参考官方文档:
--------------------------------------分割线 --------------------------------------
Python 的详细介绍:请点这里
Python 的下载地址:请点这里