Why can't I get `pip install lxml` to work within a virtualenv?

前端 未结 4 1946
春和景丽
春和景丽 2020-12-04 12:49

Note: I\'m using virtualenvwrapper.

Before activating the virtual environment:

$ pip install lxml
Requirement already satisfied (use --upgrade to upg         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 13:41

    You probably already have lxml installed on your system, perhaps installed due to a system package. Thus, the first attempt (pip install lxml without an active virtualenv) doesn't fail, but it also doesn't install it; it really doesn't do anything.

    In a virtualenv, by default, the system packages are ignored. Therefore, pip thinks that lxml is not installed. Therefore, it tries to install it into your virtual environment.

    lxml contains C modules that need to be compiled in order to install properly. However, the compilation of those C modules rely on your having some "development libraries" already installed as well. These development libraries are C libraries, not Python, and as such pip won't be able to automatically fetch them from the internet and install them for you.

    Therefore, you will need to install these development libraries on your own, most likely using your package manager. In a Debian system (like Ubuntu), this is...

    apt-get install libxml2-dev libxslt-dev
    

    This will install the libxml2 and libxslt development libraries to your local system. If you try again to install lxml, the C module compilation step should work because now these development libraries are on your system.

    The error message you were receiving was due to the fact that these libraries were missing (the libxml/xmlversion.h: No such file or directory part of the error message).

    See also: How to install lxml on Ubuntu

提交回复
热议问题