Python 如何重新安装lxml?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17766725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to re-install lxml?
提问by Mark23333
I am using python 2,7.5 on mac 10.7.5, beautifulsoup 4.2.1. I am going to parse a xml page using the lxml library, as taught in the beautifulsoup tutorial. However, when I run my code, it shows
我在 mac 10.7.5 上使用 python 2,7.5,beautifulsoup 4.2.1。我将使用 lxml 库解析一个 xml 页面,如 beautifulsoup 教程中所教。但是,当我运行我的代码时,它显示
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested:
lxml,xml. Do you need to install a parser library?
I am sure that I already installed lxml by all methods: easy_install, pip, port, etc. I tried to add a line to my code to see if lxml is installed or not:
我确定我已经通过所有方法安装了 lxml:easy_install、pip、port 等。我尝试在我的代码中添加一行以查看是否安装了 lxml:
import lxml
Then python can just successfully go through this code and display the previous error message again, occurring at the same line.
然后python可以成功地通过这段代码并再次显示之前的错误消息,发生在同一行。
So I am quite sure that lxml was installed, but not installed correctly. So I decided to uninstall lxml, and then re-install using a 'correct' method. But when I type in
所以我很确定 lxml 已安装,但未正确安装。所以我决定卸载 lxml,然后使用“正确”的方法重新安装。但是当我输入
easy_install -m lxml
it shows:
表明:
Searching for lxml
Best match: lxml 3.2.1
Processing lxml-3.2.1-py2.7-macosx-10.6-intel.egg
Using /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-
3.2.1-py2.7-macosx-10.6-intel.egg
Because this distribution was installed --multi-version, before you can
import modules from this package in an application, you will need to
'import pkg_resources' and then use a 'require()' call similar to one of
these examples, in order to select the desired version:
pkg_resources.require("lxml") # latest installed version
pkg_resources.require("lxml==3.2.1") # this exact version
pkg_resources.require("lxml>=3.2.1") # this version or higher
Processing dependencies for lxml
Finished processing dependencies for lxml
So I don't know how to continue my uninstall...
所以我不知道如何继续我的卸载...
I looked up many posts about this issue on google but still I can't find any useful info.
我在谷歌上查了很多关于这个问题的帖子,但仍然找不到任何有用的信息。
Here is my code:
这是我的代码:
import mechanize
from bs4 import BeautifulSoup
import lxml
class count:
def __init__(self,protein):
self.proteinCode = protein
self.br = mechanize.Browser()
def first_search(self):
#Test 0
soup = BeautifulSoup(self.br.open("http://www.ncbi.nlm.nih.gov/protein/21225921?report=genbank&log$=prottop&blast_rank=1&RID=YGJHMSET015"), ['lxml','xml'])
return
if __name__=='__main__':
proteinCode = sys.argv[1]
gogogo = count(proteinCode)
I want to know:
我想知道:
- How can I uninstall lxml?
- How can I install lxml 'correctly'? How do I know that it is correctly installed?
- 如何卸载 lxml?
- 如何“正确”安装 lxml?我怎么知道它是否正确安装?
回答by osa
I am using BeautifulSoup 4.3.2 and OS X 10.6.8. I also have a problem with improperly installed lxml
. Here are some things that I found out:
我使用的是 BeautifulSoup 4.3.2 和 OS X 10.6.8。我也有安装不正确的问题lxml
。以下是我发现的一些事情:
First of all, check this related question: Removed MacPorts, now Python is broken
首先,检查这个相关的问题:Removed MacPorts, now Python is crashed
Now, in order to check which builders for BeautifulSoup 4 are installed, try
现在,为了检查安装了 BeautifulSoup 4 的哪些构建器,请尝试
>>> import bs4
>>> bs4.builder.builder_registry.builders
If you don't see your favorite builder, then it is not installed, and you will see an error as above ("Couldn't find a tree builder...").
如果你没有看到你最喜欢的构建器,那么它没有安装,你会看到如上的错误(“找不到树构建器......”)。
Also, just because you can import lxml
, doesn't mean that everything is perfect.
此外,仅仅因为您可以import lxml
,并不意味着一切都是完美的。
Try
尝试
>>> import lxml
>>> import lxml.etree
To understand what's going on, go to the bs4
installation and open the egg (tar -xvzf
). Notice the modules bs4.builder
. Inside it you should see files such as _lxml.py
and _html5lib.py
. So you can also try
要了解发生了什么,请转到bs4
安装并打开鸡蛋 ( tar -xvzf
)。注意模块bs4.builder
。在其中,您应该会看到诸如_lxml.py
和 之类的文件_html5lib.py
。所以你也可以试试
>>> import bs4.builder.htmlparser
>>> import bs4.builder._lxml
>>> import bs4.builder._html5lib
If there is a problem, you will see, why a parricular module cannot be loaded. You can notice how at the end of builder/__init__.py
it loads all those modules and ignores whatever was not loaded:
如果有问题,您将看到为什么无法加载特定模块。您可以注意到builder/__init__.py
它最后如何加载所有这些模块并忽略未加载的任何内容:
# Builders are registered in reverse order of priority, so that custom
# builder registrations will take precedence. In general, we want lxml
# to take precedence over html5lib, because it's faster. And we only
# want to use HTMLParser as a last result.
from . import _htmlparser
register_treebuilders_from(_htmlparser)
try:
from . import _html5lib
register_treebuilders_from(_html5lib)
except ImportError:
# They don't have html5lib installed.
pass
try:
from . import _lxml
register_treebuilders_from(_lxml)
except ImportError:
# They don't have lxml installed.
pass
回答by Michael
apt-get on Debian/Ubuntu:
sudo apt-get install python3-lxml
For MacOS-X, a macport of lxml is available. Try something like
sudo port install py27-lxml
Debian/Ubuntu 上的 apt-get:
sudo apt-get install python3-lxml
对于 MacOS-X,可以使用 lxml 的 macport。尝试类似
sudo port install py27-lxml
http://lxml.de/installation.htmlmay be helpful.
http://lxml.de/installation.html可能会有所帮助。
回答by Mona Jalal
If you are using Python2.7 in Ubuntu/Debian, this worked for me:
如果您在 Ubuntu/Debian 中使用 Python2.7,这对我有用:
$ sudo apt-get build-dep python-lxml
$ sudo pip install lxml
Test it like:
测试它像:
mona@pascal:~/computer_vision/image_retrieval$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
回答by basse
FWIW, I ran into a similar problem (python 3.6, os x 10.12.6) and was able to solve it simply by doing (first command is just to signify that I was working in a conda virtualenv):
FWIW,我遇到了类似的问题(python 3.6,os x 10.12.6)并且能够简单地通过执行来解决它(第一个命令只是表示我在 conda virtualenv 中工作):
$ source activate ml-general
$ pip uninstall lxml
$ pip install lxml
I tried more complicated things first, because BeautifulSoup was working correctly with an identical command through Jupyter+iPython, but not through PyCharm's terminal in the same virtualenv. Simply reinstalling lxml as above solved the problem.
我首先尝试了更复杂的事情,因为 BeautifulSoup 可以通过 Jupyter+iPython 使用相同的命令正常工作,但不能通过 PyCharm 的终端在同一个 virtualenv 中正常工作。只需按照上述方法重新安装 lxml 即可解决问题。