Import error for lxml in python

故事扮演 提交于 2019-12-04 04:08:06

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.

A good way to make a clean environment available is to use virtualenv :

$ virtualenv myproject
$ cd myproject
$ ./bin/pip install lxml # Repeat this with other dependencies
[wait for download and compiling]

Then, use ./bin/python to execute your script. The advantages of this method are :

  • 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

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.

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.

So, i just removed the python path of the software from the path variable of bash.

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.

In my case upgrading from lxml 3.3.5 to 4.2.5 fixed the problem.

For Python 3.6, it is very tricky.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!