Как установить lxml python 3
Перейти к содержимому

Как установить lxml python 3

  • автор:

Where to get it

Most Linux platforms come with some version of lxml readily packaged, usually named python-lxml for the Python 2.x version and python3-lxml for Python 3.x. If you can use that version, the quickest way to install lxml is to use the system package manager, e.g. 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

To install a newer version or to install lxml on other systems, see below.

Requirements

You need Python 3.6+ for lxml 5.0 and later. lxml versions before 5.0 support Python 2.7 and 3.6+.

Unless you are using a static binary distribution (e.g. from a Windows binary installer), lxml requires libxml2 and libxslt to be installed, in particular:

  • libxml2 version 2.9.2 or later.
  • libxslt version 1.1.27 or later.
    • We recommend libxslt 1.1.28 or later.

    Newer versions generally contain fewer bugs and are therefore recommended. XML Schema support is also still worked on in libxml2, so newer versions will give you better compliance with the W3C spec.

    To install the required development packages of these dependencies on Linux systems, use your distribution specific installation tool, e.g. apt-get on Debian/Ubuntu:

    sudo apt-get install libxml2-dev libxslt-dev python-dev

    For Debian based systems, it should be enough to install the known build dependencies of the provided lxml package, e.g.

    sudo apt-get build-dep python3-lxml

    Installation

    If your system does not provide binary packages or you want to install a newer version, the best way is to get the pip package management tool (or use a virtualenv) and run the following:

    pip install lxml

    If you are not using pip in a virtualenv and want to install lxml globally instead, you have to run the above command as admin, e.g. on Linux:

    sudo pip install lxml

    To install a specific version, either download the distribution manually and let pip install that, or pass the desired version to pip:

    pip install lxml==5.0.0

    To speed up the build in test environments, e.g. on a continuous integration server, disable the C compiler optimisations by setting the CFLAGS environment variable:

    CFLAGS="-O0" pip install lxml

    (The option reads «minus Oh Zero», i.e. zero optimisations.)

    MS Windows

    For MS Windows, we try to provide binary wheels with reasonably up-to-date libraries, although you might still want to take a look at the related FAQ entry. Since it is generally difficult to build software on Windows, the library versions (libxml2, libxslt, libiconv, zlib) might not always be at the same version level as the builds on Linux or macOS. This usually means that the WinLibs project has not updated their repositories yet. If you need a more recent version, please file a ticket on their side to update it.

    Linux

    On Linux (and most other well-behaved operating systems), pip will manage to build the source distribution as long as libxml2 and libxslt are properly installed, including development packages, i.e. header files, etc. See the requirements section above and use your system package management tool to look for packages like libxml2-dev or libxslt-devel . If the build fails, make sure they are installed.

    Alternatively, setting STATIC_DEPS=true will download and build both libraries automatically in their latest version, e.g. STATIC_DEPS=true pip install lxml.

    MacOS-X

    On MacOS-X, use the following to build the source distribution, and make sure you have a working Internet connection, as this will download libxml2 and libxslt in order to build them:

    STATIC_DEPS=true sudo pip install lxml

    Building lxml from dev sources

    If you want to build lxml from the GitHub repository, you should read how to build lxml from source (or the file doc/build.txt in the source tree). Building from developer sources or from modified distribution sources requires Cython to translate the lxml sources into C code. The source distribution ships with pre-generated C source files, so you do not need Cython installed to build from release sources.

    If you have read these instructions and still cannot manage to install lxml, you can check the archives of the mailing list to see if your problem is known or otherwise send a mail to the list.

    Using lxml with python-libxml2

    If you want to use lxml together with the official libxml2 Python bindings (maybe because one of your dependencies uses it), you must build lxml statically. Otherwise, the two packages will interfere in places where the libxml2 library requires global configuration, which can have any kind of effect from disappearing functionality to crashes in either of the two.

    To get a static build, either pass the --static-deps option to the setup.py script, or run pip with the STATIC_DEPS or STATICBUILD environment variable set to true, i.e.

    STATIC_DEPS=true pip install lxml

    The STATICBUILD environment variable is handled equivalently to the STATIC_DEPS variable, but is used by some other extension packages, too.

    Source builds on MS Windows

    Most MS Windows systems lack the necessarily tools to build software, starting with a C compiler already. Microsoft leaves it to users to install and configure them, which is usually not trivial and means that distributors cannot rely on these dependencies being available on a given system. In a way, you get what you’ve paid for and make others pay for it.

    Due to the additional lack of package management of this platform, it is best to link the library dependencies statically if you decide to build from sources, rather than using a binary installer. For that, lxml can use the binary distribution of libxml2 and libxslt, which it downloads automatically during the static build. It needs both libxml2 and libxslt, as well as iconv and zlib, which are available from the same download site. Further build instructions are in the source build documentation.

    Source builds on MacOS-X

    If you are not using macports or want to use a more recent lxml release, you have to build it yourself. While the pre-installed system libraries of libxml2 and libxslt are less outdated in recent MacOS-X versions than they used to be, so lxml should work with them out of the box, it is still recommended to use a static build with the most recent library versions.

    Luckily, lxml’s setup.py script has built-in support for building and integrating these libraries statically during the build. Please read the MacOS-X build instructions.

    Installing lxml

    For special installation instructions regarding MS Windows and MacOS-X, see the specific sections below.

    • Requirements
    • Installation
    • Building lxml from sources
    • Using lxml with python-libxml2
    • MS Windows
    • MacOS-X

    Requirements

    You need Python 2.4 or later.

    Unless you are using a static binary distribution (e.g. from a Windows binary installer), you need to install libxml2 and libxslt, in particular:

    • libxml2 2.6.21 or later. It can be found here: http://xmlsoft.org/downloads.html
      • We recommend libxml2 2.7.8 or a later version.
      • If you want to use XPath, do not use libxml2 2.6.27.
      • If you want to use the feed parser interface, especially when parsing from unicode strings, do not use libxml2 2.7.4 through 2.7.6.
      • We recommend libxslt 1.1.26 or later.

      Newer versions generally contain fewer bugs and are therefore recommended. XML Schema support is also still worked on in libxml2, so newer versions will give you better compliance with the W3C spec.

      Installation

      Unless you are on MS Windows, the best way to install lxml is to get the pip package management tool and run the following as super-user (or administrator):

      pip install lxml

      To install a specific version, either download the distribution manually and let pip install that, or pass the desired version to pip:

      pip install lxml==2.3
      • For MS Windows, we no longer provide binary distributions. Also see the related FAQ entry. If you fail to build lxml on your MS Windows system from the signed and tested sources that we release, consider using the unofficial Windows binaries that Christoph Gohlke generously provides.
      • On Linux (and most other well-behaved operating systems), pip will manage to build the source distribution as long as libxml2 and libxslt are properly installed, including development packages, i.e. header files, etc. Use your package management tool to look for packages like libxml2-dev or libxslt-devel if the build fails, and make sure they are installed. Alternatively, setting STATIC_DEPS=true will download and build both libraries automatically.
      • On MacOS-X, use the following to build the source distribution, and make sure you have a working Internet connection, as this will download libxml2 and libxslt in order to build them:
      STATIC_DEPS=true sudo pip install lxml

      Building lxml from sources

      If you want to build lxml from the GitHub repository, you should read how to build lxml from source (or the file doc/build.txt in the source tree). Building from developer sources or from modified distribution sources requires Cython to translate the lxml sources into C code. The source distribution ships with pre-generated C source files, so you do not need Cython installed to build from release sources.

      If you have read these instructions and still cannot manage to install lxml, you can check the archives of the mailing list to see if your problem is known or otherwise send a mail to the list.

      Using lxml with python-libxml2

      If you want to use lxml together with the official libxml2 Python bindings (maybe because one of your dependencies uses it), you must build lxml statically. Otherwise, the two packages will interfere in places where the libxml2 library requires global configuration, which can have any kind of effect from disappearing functionality to crashes in either of the two.

      To get a static build, either pass the --static-deps option to the setup.py script, or run pip with the STATIC_DEPS or STATICBUILD environment variable set to true, i.e.

      STATIC_DEPS=true pip install lxml

      The STATICBUILD environment variable is handled equivalently to the STATIC_DEPS variable, but is used by some other extension packages, too.

      MS Windows

      Most MS Windows systems lack the necessarily tools to build software, starting with a C compiler already. Microsoft leaves it to users to install and configure them, which is usually not trivial and means that distributors cannot rely on these dependencies being available on a given system. In a way, you get what you’ve paid for and make others pay for it.

      Due to the additional lack of package management of this platform, it is best to link the library dependencies statically if you decide to build from sources. For that, use the binary distribution of libxml2 and libxslt. You need both libxml2 and libxslt, as well as iconv and zlib, which you can get from the same download site. Further build instructions are in the source build documentation.

      MacOS-X

      A macport of lxml is available. Try something like port install py25-lxml .

      If you want to use a more recent lxml release, you may have to build it yourself. Apple doesn’t help here, as MacOS-X is so badly maintained by them that the pre-installed system libraries of libxml2 and libxslt tend to be horribly outdated, and updating them is everything but easy. In any case, you cannot run lxml with the system provided libraries, so you have to use newer libraries.

      Luckily, lxml’s setup.py script has built-in support for building and integrating these libraries statically during the build. Please read the MacOS-X build instructions.

      A number of users also reported success with updated libraries (e.g. using fink or macports), but needed to set the runtime environment variable DYLD_LIBRARY_PATH to the directory where fink keeps the libraries. In any case, this method is easy to get wrong and everything but safe. Unless you know what you are doing, follow the static build instructions above.

      Python. Ошибка при установки пакета LXML, в консоли пишу «pip install lxml», использую OS Windows. Что делать?

      Нашел рекомендацию вначале надо установить зависимости:
      apt-get install libxml2-dev libxslt-dev python-dev lib32z1-dev
      Но как это сделать не понимаю.

      • Вопрос задан более трёх лет назад
      • 6597 просмотров

      5 комментариев

      Оценить 5 комментариев

      ivsol

      Что за ошибка?

      Witosser

      Виктор Юрченко @Witosser Автор вопроса
      ошибка error: Unable to find vcvarsall.bat

      Witosser

      Виктор Юрченко @Witosser Автор вопроса

      И текстовое сообщение
      Command «c:\users\витор\appdata\local\programs\python\python35-32\python.exe -u -c «import setuptools, tokenize;__file__=’C:\\Users\\C68B~1\\AppData\\Local\\Temp\\pip-build-zhflna4z\\lxml\\setup.py’;exec(compile(getattr(tokenize, ‘open’, open)(__file__).read().replace(‘\r\n’, ‘\n’), __file__, ‘exec’))» install —record C:\Users\C68B~1\AppData\Local\Temp\pip-csn4z864-record\install-record.txt —single-version-externally-managed —compile» failed with error code 1 in C:\Users\C68B~1\AppData\Local\Temp\pip-build-zhflna4z\lxml\

      Как установить lxml в виртуальное окружение PyCharm

      У меня: Python 3.8.1 Windows 10 — 64 На PyCharm’е в виртуальное окружение пытаюсь поставить lxml:

      (venv) F:\testProjekt\Fs_h>pip install lxml 

      Получаю в конце примерно следующее:

       running build_ext building 'lxml.etree' extension error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": https://visualstudio.microsoft.com/downloads/ ---------------------------------------- Command "F:\testProjekt\Fs_h\venv\Scripts\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\popkov\\AppData\\Local\\Temp\\pip-install-mcl3hk8o\\lxml\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read( ).replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\popkov\AppData\Local\Temp\pip-record-4b6h6qyg\install-record.txt --single-version-externally-managed --compile --install-headers F:\te stProjekt\Fs_h\venv\include\site\python3.8\lxml" failed with error code 1 in C:\Users\popkov\AppData\Local\Temp\pip-install-mcl3hk8o\lxml\ 

      По ссылке из ошибки открывается страница для скачивания каких то студий, никогда ими не пользовался, но одна установлена зачем то, в общем глобально(не в окружение) lxml нормально ставится(сюда «C:\Python38-32\Lib\site-packages\lxml») я даже пользовался ничего не обычного работает, а в окружение никак( Вопрос у меня следующий: Как мне еще можно поставить lxml именно в окружение? Может вручню что докинуть или еще какой установщик попробовать? Дополнительная информация: На одном форуме посоветовали установить wheel от сюда https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml . Пытался ставить несколько штук, ради эксперимента и 32х пробовал, но ответ всегда был один:

      (venv) F:\testProjekt\Fs_h>pip install lxml-4.4.3-cp38-cp38-win_amd64.whl lxml-4.4.3-cp38-cp38-win_amd64.whl is not a supported wheel on this platform. 

      Отслеживать

      задан 8 июл 2020 в 13:53

      Сергей Попков Сергей Попков

      423 3 3 серебряных знака 17 17 бронзовых знаков

      lxml основан на одноименное бинарной библиотеке. Для ее компиляции требуется набор компиляторов и библиотек. Именно это вам и предлагают скачать и установить — «Microsoft Visual C++ Build Tools». Можете смело качать и ставить.

      8 июл 2020 в 14:00

      По поводу wheel могли бы хотя бы погуглить. Есть же ответ вот здесь и вот здесь

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *