python intel64 windows 安装lxml

1. pip install lxml

从下载日志看,下载地址是:

https://pypi.tuna.tsinghua.edu.cn/packages/52/7f/aeaa0064809c319078b97bd30a0d7f7ee062df07608fa439029a948a431e/lxml-4.2.1-cp27-cp27m-win_amd64.whl

但是 执行 

from lxml import html 的时候报:
ImportError: cannot import name html

最后判断应该是我电脑的cpu是intel的,但是下载的是amd的。

ps: 我使用pip源是: https://pypi.tuna.tsinghua.edu.cn/simple


2.  不得已,只能通过源码安装了。

使用命令 pip install lxml==2.3 安装时报如下错误:

 building 'lxml.etree' extension

 error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27

在如下网站中:https://www.microsoft.com/en-us/download/confirmation.aspx?id=44266下载

Microsoft Visual C++ Compiler for Python 2.7 即可.

安装完lxml后,又报如下错误:

c:\users\thinkpad\appdata\local\temp\pip-install-c70gbr\lxml\src\lxml\etree_defs.h(9) : fatal error C1083: Cannot open include file: 'libxml/xmlversion.h': No such file or directory

    error: command 'C:\\Users\\ThinkPad\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\amd64\\cl.exe' failed with exit status 2

这个方法行不通,最后通过:

http://lxml.de/installation.html#source-builds-on-ms-windows

Most MS Windows systems lack the necessarily tools to build software, starting with a C compiler already. Microsoft leaves it to users to install and configure them, which is usually not trivial and means that distributors cannot rely on these dependencies being available on a given system. In a way, you get what you've paid for and make others pay for it.

Due to the additional lack of package management of this platform, it is best to link the library dependencies statically if you decide to build from sources, rather than using a binary installer. For that, lxml can use the binary distribution of libxml2 and libxslt, which it downloads automatically during the static build. It needs both libxml2 and libxslt, as well as iconv and zlib, which are available from the same download site. Further build instructions are in the source build documentation.


找到需要下载lxml依赖的c库,下载地址:

ftp://ftp.zlatkovic.com/libxml/64bit/

下载完依赖包: 

iconv-1.14-win32-x86_64.7z ,  

libxml2-2.9.3-win32-x86_64.7z , 

libxslt-1.1.28-win32-x86_64.7z,

zlib-1.2.8-win32-x86_64.7z

并从 http://lxml.de/index.html#download下载lxml 4.2.1源码;

这之后,目录结构如下,

python intel64 windows 安装lxml

执行:

cd  E:\life\tech\python\lxml\dist\lxml-4.2.1

python setup.py build_ext -i --without-cython --with-xslt-config=E:\life\tech\python\lxml\bin\xslt-config

执行结果:

python intel64 windows 安装lxml

从上面的错误可以看出xslt-config不是可执行文件,从:

ftp://ftp.zlatkovic.com/libxml/64bit/readme.txt

中看到,我下载的libxslt使用MinW工具链编译的,看来编译源码这条路也只能放过了。

(c语言编译这块还是弱,有时间将这块补上。)

 官方windows源码安装方法:  http://lxml.de/build.html


3. 上面两条路不通,我想是不是python版本的问题。结果换了个python版本还是没有解决。

但意外的是发现,通过在命令行里执行 from lxml import html是ok的, 如下图:

python intel64 windows 安装lxml

对比第一条的错误:

python intel64 windows 安装lxml

这个一比较,不会环境配置的事吧。

然后在命令行执行:

python E:\workspace\python_workspace\test\chpt2\lxml.py 依旧报错

python intel64 windows 安装lxml

唯一不报错的方法:

console中执行 或者 vscode中切到c盘:

python

execfile('E:/workspace/python_workspace/test/chpt2/lxml.py')

python intel64 windows 安装lxml

目前总算是可以执行包含lxml库的单文件了。但是如果需要调试python文件的话还是有问题

补充:

目前总算是知道问题的真正所在了:

python lxml.py

的时候由于lxml.py与已经加载的lxml模块同名,所以会优先加载我自己写的lxml, 但是我自己写的lxml并没有html方法,所以才

会有这个问题。

所以最后的解决方法:

将lxml.py 重新命名为非系统模块名称就好。


----2018-5-13 00:00:00