python中lxml的导入错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14241685/
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
Import error for lxml in python
提问by user1254498
I wrote a script some times ago that contain
我前段时间写了一个脚本,其中包含
from lxml import etree
But, unfortunatly it is not working anymore. In doubt i checked installation with :
但是,不幸的是它不再起作用了。有疑问,我检查了安装:
sudo apt-get install python-lxml
sudo pip install lxml
sudo apt-get install libxml2-dev
sudo apt-get install libxslt1-dev
I checked if it could be my python version with :
我检查了它是否可能是我的 python 版本:
me@pc:~$ python
Python 2.7.3 (default, Sep 14 2012, 14:11:57)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named lxml
My os is ubuntu 12.04.1 LTS with Python 2.7.3.
我的操作系统是带有 Python 2.7.3 的 ubuntu 12.04.1 LTS。
All seems fine. I can't see what could be the problem.
一切似乎都很好。我看不出是什么问题。
SOLVED:
解决了:
Finally importing etree with
最后导入 etree
from xml import etree
Don't know why and if there is a difference but it's working as expected.
不知道为什么以及是否存在差异,但它按预期工作。
采纳答案by user1254498
Solved the problem.
解决了问题。
It seems that a software that i installed messed out my python path. The python that i was using when calling python in a terminal was the one installed by the software, and the one called by my script was the one installed on my system.
我安装的软件似乎弄乱了我的python路径。我在终端调用python时使用的python是软件安装的python,脚本调用的python是我系统上安装的python。
So, i just removed the python path of the software from the path variable of bash.
所以,我只是从bash的路径变量中删除了软件的python路径。
回答by Alexis Huet
Your solution cited in edit, which use the xml.etree instead of lxml.etree is not the better way to do it, as these module have known incompatibilities, and mainly because lxml is certainly more optimised.
您在编辑中引用的解决方案,使用 xml.etree 而不是 lxml.etree 并不是更好的方法,因为这些模块已知不兼容,主要是因为 lxml 肯定更优化。
A good way to make a clean environment available is to use virtualenv:
提供干净环境的一个好方法是使用virtualenv:
$ virtualenv myproject
$ cd myproject
$ ./bin/pip install lxml # Repeat this with other dependencies
[wait for download and compiling]
Then, use ./bin/pythonto execute your script. The advantages of this method are :
然后,使用./bin/python执行您的脚本。这种方法的优点是:
- you can have different versions of dependencies between your system and your project
- even if you break everything in your virtual environment, you will not compromised the rest of your system
- you do not need root privileges to make an installation
- 您的系统和项目之间可以有不同版本的依赖项
- 即使您破坏了虚拟环境中的所有内容,也不会破坏系统的其余部分
- 您不需要 root 权限即可进行安装
As a reference, a more powerful but slightly more complex way to achieve this is to use buildout, but it can looks like hunting flies with a bazooka if you just want to execute a simple one-file script.
作为参考,实现这一点的一种更强大但稍微复杂的方法是使用buildout,但如果您只想执行一个简单的单文件脚本,它看起来就像用火箭筒打苍蝇。
回答by damonxu
For Python 3.6, it is very tricky.
对于 Python 3.6,这是非常棘手的。
I have the same problem and try to install the latest version, lxml 3.8. But I still get the error. And then I use lxml 3.7.3, then it is ok.
我有同样的问题并尝试安装最新版本 lxml 3.8。但我仍然收到错误。然后我用lxml 3.7.3,然后就ok了。
回答by James Bradbury
I had this problem when running tests in PyCharm. I could import lxml when using the terminal and virtual environment, but using the same virtual environment to run the tests resulted in the ImportError.
在 PyCharm 中运行测试时遇到了这个问题。我可以在使用终端和虚拟环境时导入 lxml,但是使用相同的虚拟环境来运行测试导致了 ImportError。
In my case upgrading from lxml 3.3.5 to 4.2.5 fixed the problem.
在我的情况下,从 lxml 3.3.5 升级到 4.2.5 解决了这个问题。

