新建python環境無法導入libxml2dom的問題:ImportError: No module named libxmlmods

最近一個python項目需要用到libxml2dom這個包,然後就pip安裝之,之後驗證安裝是否成功:

shandow@mac:~ > python

Python 2.7.5 (default, Mar  9 2014, 22:15:05) 

[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import libxml2dom

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "/Library/Python/2.7/site-packages/libxml2dom/__init__.py", line 24, in <module>

    from libxml2dom.macrolib import *

  File "/Library/Python/2.7/site-packages/libxml2dom/macrolib/__init__.py", line 26, in <module>

    from libxml2dom.macrolib.macrolib import *

  File "/Library/Python/2.7/site-packages/libxml2dom/macrolib/macrolib.py", line 30, in <module>

    from libxmlmods import libxml2mod

ImportError: No module named libxmlmods

意思是缺乏libxmlmods庫,使用pip安裝之,提示找不到。。。。好吧,度娘問吧,找到一個類似libxml2dom的官方聲明似的網站:

libxml2dom

Current release: libxml2dom 0.5 (requiring the low-level libxml2 Python bindings, typically provided by the python-libxml2 or libxml2-python packages for various GNU/Linux distributions: UbuntuDebianFedora, Red Hat, SuSE)

Introduction

The libxml2dom package provides a traditional DOM wrapper around the Python bindings for libxml2. In contrast to the standard libxml2 bindings, libxml2dom provides an API reminiscent of minidompxdom and other Python-based and Python-related XML toolkits. Performance is fairly respectable since libxml2dom makes direct use of libxml2mod - the low-level wrapping of libxml2 for Python. Moreover, serialisation of documents is much faster than many other toolkits because libxml2dom can make direct use of libxml2 rather than employing Python-level mechanisms to visit and serialise nodes.

Copyright and Licence

libxml2dom is licensed under the LGPL version 3 (or later).



這裏第一句話就是libxml2dom依賴libxml2庫,可以通過查找python-libxml2或者libxml2-python下載,可是偶使用pip安裝都提示找不到資源。。。。好吧,再次度娘,手動下載了這個包。這裏貼出我分享出來的地址:libxml2下載(我的是mac os系統,windows系統的自行下載吧,哈哈。。。)再說下mac os下如何安裝:

1.先把下載的tar包放到自己python環境的site-packages,我的是:

/Library/Python/2.7/site-packages

2.解壓:sudo tar -xvf libxml2-2.7.8.tar

3.進入解壓後的文件夾,由於是源碼包需要編譯安裝:

cd libxml2-2.7.8

sudo ./configure

sudo make 

sudo make install

好了,如果沒有報錯就ok了,測試下把:

python

import libxml2

提示沒有這個模塊,說明沒有導成功,鬱悶。。。。

重新進入文件夾  :

cd libxml2-2.7.8  

發現這個文件夾中有個python文件夾,進入:

shandow@mac:/Library/Python/2.7/site-packages/libxml2-2.7.8 > cd python/

shandow@mac:/Library/Python/2.7/site-packages/libxml2-2.7.8/python > ll

total 5704

-rw-r--r--   1 root   network     132 11 17 10:46 MANIFEST

-rw-r--r--   1 root   network   31357 11 17 10:43 Makefile

-rw-rw-r--@  1 50138  network    1542 11  4  2010 Makefile.am

-rw-rw-r--@  1 50138  network   32443 11  5  2010 Makefile.in

-rw-rw-r--@  1 50138  network    1272  9 24  2009 README

-rw-rw-r--@  1 50138  network    1623  9 24  2009 TODO

drwxr-xr-x   4 root   network     136 11 17 10:46 build

-rw-rw-r--@  1 50138  network   15061  9 24  2009 drv_libxml2.py

-rw-r--r--   1 root   network       0 11 17 10:44 gen_prog

-rwxrwxr-x@  1 50138  network   47541 10 16  2010 generator.py

-rw-rw-r--@  1 50138  network  104464 11  3  2010 libxml.c

-rw-r--r--   1 root   network     271 11 17 10:44 libxml.lo

-rw-r--r--   1 root   network  297240 11 17 10:44 libxml.o

-rw-rw-r--@  1 50138  network   22817 10 12  2010 libxml.py

-rw-r--r--   1 root   network  126532 11 17 10:44 libxml2-export.c

-rw-r--r--   1 root   network  434813 11 17 10:44 libxml2-py.c

-rw-r--r--   1 root   network  112512 11 17 10:44 libxml2-py.h

-rw-r--r--   1 root   network     283 11 17 10:44 libxml2-py.lo

-rw-r--r--   1 root   network  819684 11 17 10:44 libxml2-py.o

-rw-rw-r--@  1 50138  network   18669 10 12  2010 libxml2-python-api.xml

-rw-r--r--   1 root   network  341257 11 17 10:44 libxml2.py

-rw-r--r--   1 root   network  318440 11 17 10:44 libxml2class.py

-rw-r--r--   1 root   network   22768 11 17 10:44 libxml2class.txt

-rw-r--r--   1 root   network    1166 11 17 10:44 libxml2mod.la

-rw-rw-r--@  1 50138  network    7277 10 12  2010 libxml_wrap.h

-rwxr-xr-x   1 root   network    6685 11 17 10:43 setup.py

-rwxrwxr-x@  1 50138  network    6707  9 24  2009 setup.py.in

drwxrwxr-x@ 55 50138  network    1870 11 17 10:43 tests

-rw-rw-r--@  1 50138  network   21068 10 12  2010 types.c

-rw-r--r--   1 root   network     268 11 17 10:44 types.lo

-rw-r--r--   1 root   network   63320 11 17 10:44 types.o

shandow@mac:/Library/Python/2.7/site-packages/libxml2-2.7.8/python > 


參照網上說的python第三方包的安裝方式:

sudo python setup.py build

sudo python setup.py install

這兩句運行完後,重新進入site-packages文件夾,發現多了libxml2的egg-info文件:


再次測試:

shandow@mac:/Library/Python/2.7/site-packages > python

Python 2.7.5 (default, Mar  9 2014, 22:15:05) 

[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> import libxml2

>>> import libxml2dom

>>> 

libxml2安裝成功了,libxml2dom也不會報錯了,大功告成。。。。。網上百度,google都沒有這個問題的解決辦法,作者這裏原創一個,哈哈。。。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章