- Installing lxml on Mac OSX (10.11) inside a virtualenv with pip
- Installing lxml
- Requirements
- Installation
- MS Windows
- Linux
- MacOS-X
- Building lxml from dev sources
- Using lxml with python-libxml2
- Source builds on MS Windows
- Source builds on MacOS-X
- Need help installing lxml on os x 10.7
- 1 Answer 1
- How do you install lxml on OS X Leopard without using MacPorts or Fink?
- 15 Answers 15
- Cannot install Lxml on Mac OS X 10.9
- 23 Answers 23
Installing lxml on Mac OSX (10.11) inside a virtualenv with pip
The lxml library is a dependency for many popular Python projects such as Scrapy and Nikola. If you are a Mac OSX user, it’s highly likely that you have encountered the following error when trying to install lxml or its dependants with pip:
For a long time, the easy fix for this was to simply set the env var
which makes the setup.py for lxml download and build the latest versions of libxml2 and libxslt instead of using the system version, as suggested by the official installation instructions and this StackOverflow answer:
This was all fine until 20 Nov 2015, when version 2.9.3 of libxml2 was released. When installing with the above command, you will encounter an exception similar to this:
with the compilation error somewhere above in the traceback message:
This seems to be an error introduced in version 2.9.3. For now, a reasonable workaround is to install lxml as before, but explicitly specify the version of lxml to use (version 2.9.2). In another StackOverflow answer, they manually download lxml and run setup.py with flags —static-deps , —libxml2-version and libxslt-version .
Note that we can still use pip (only version >= 7.0) and propagate the options to setup.py with —install-option :
Alternatively, if you must us an older version of pip that does not support passing options, the setup.py for lxml is implemented to support passing command line options as environment variables (see setupinfo.py). This is why we were able to simply set STATIC_DEPS=true before.
In the same manner, we can just set LIBXML2_VERSION=2.9.2:
Now lxml should be successfully installed:
Источник
Installing lxml
lxml is generally distributed through PyPI.
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:
For MacOS-X, a macport of lxml is available. Try something like
To install a newer version or to install lxml on other systems, see below.
Requirements
You need Python 2.7 or 3.4+.
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:
For Debian based systems, it should be enough to install the known build dependencies of the provided lxml package, e.g.
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:
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:
To install a specific version, either download the distribution manually and let pip install that, or pass the desired version to pip:
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:
(The option reads «minus Oh Zero», i.e. zero optimisations.)
MS Windows
For MS Windows, recent lxml releases feature community donated binary distributions, although you might still want to take a look at 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 binary builds from PyPI or the unofficial Windows binaries that Christoph Gohlke generously provides.
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:
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.
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.
Источник
Need help installing lxml on os x 10.7
I have been struggling to be able to do from lxml import etree ( import lxml works fine by the way) The error is:
i used pip to install lxml, and homebrew to reinstall libxml2 with the right architecture (or so i think) . does anyone have ideas on how to fix/diagnose this? I’m on 64 bit python
1 Answer 1
lxml is a bit fussy about what 3rd-party libraries it uses and it often needs newer versions than what are supplied by Apple. Suggest you read and follow the instructions here for building lxml from source on Mac OS X including building its own statically linked libs. That should work. (I’m a little surprised that homebrew doesn’t already have an lxml recipe.)
UPDATE: Based on the limited information in your comments, it is difficult to be sure exactly what is happening. I suspect you are not using the version of Python you think you are. There are any number of ways to install lxml successfully; that’s part of the problem: there are too many options. Rather than trying to debug your setup, here’s probably the simplest way to get a working lxml on 10.7 using the Apple-supplied system Python 2.7.
You should then be able to use lxml.etree this way:
I notice though that the lxml static build process does not produce a working universal build. You’ll probably see messages like this during the lxml install:
Assuming the default architecture on your machine is 64-bits, if you try to run in 32-bit mode:
And there is the error message you originally reported! So the root cause of that appears to be that the static libraries ( libxml2 etc) that lxml builds are not universal. As long as you have no need to use lxml in a 32-bit process (unlikely for most uses), this should not be a problem. Chances are that the Python you were originally using was a 32-bit-only one; that is consistent with some of the other messages you reported.
Источник
How do you install lxml on OS X Leopard without using MacPorts or Fink?
I’ve tried this and run in to problems a bunch of times in the past. Does anyone have a recipe for installing lxml on OS X without MacPorts or Fink that definitely works?
Preferably with complete 1-2-3 steps for downloading and building each of the dependencies.
15 Answers 15
Thanks to @jessenoller on Twitter I have an answer that fits my needs — you can compile lxml with static dependencies, hence avoiding messing with the libxml2 that ships with OS X. Here’s what worked for me:
This worked for me (10.6.8):
I’ve had excellent luck with Homebrew to install the libxml2 dependency:
Homebrew doesn’t seem to have libxslt available, but I’ve not yet had a need for XSLT. YMMV.
Once you have the dependency(s), then the usual methods work just fine:
Easy_install can work using this:
STATIC_DEPS=true easy_install ‘lxml>=2.2beta4’
you may then need to run, depending on permissions;
STATIC_DEPS=true sudo easy_install ‘lxml>=2.2beta4’
using homebrew (0.9.5) on el capitan (10.11.1) the following worked for me:
This worked for me on 10.8.5
- Install Xcode from Mac App Store
- Xcode -> Preferences -> Downloads -> Command Line Tools
- Install homebrew using
- ruby -e «$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)»
- brew install libxml2
- sudo easy_install lxml
This comprises suggestions from:
But I wanted to compile it into one answer rather than leave comments everywhere
On OS X 10.9.1 the suggested answer above errors out during install — following changes had to be made:
I had this working fine with Snow Lepoard but after I upgraded to Lion I had to symlink gcc-4.2 to gcc. Running sudo env ARCHFLAGS=»-arch i386 -arch x86_64″ easy_install lxml was looking for gcc-4.2 instead of gcc.
I’m using OSX 10.11 El Capitan and Homebrew. Using pip install lxml would give me «fatal error: ‘libxml/xmlversion.h’ file not found» and «failed with error code 1» blah blah.
According to the official website, I should use STATIC_DEPS=true pip install lxml (add sudo before pip if you need that), and that solved my problem.
I ran brew install libxml2 and brew install libxslt to install the dependencies while troubleshooting. I’m not sure if those two commands are necessary.
Try installing Cython and installing from source, easy_install does fail. I haven’t tried on my mac yet though.
Failing that the ports version isn’t that ancient. You can see the dependencies, some of which had to be updated for my Linux build of lxml.
info py25-lxml py25-lxml @2.1.5 (python, devel)
Источник
Cannot install Lxml on Mac OS X 10.9
I want to install Lxml so I can then install Scrapy.
When I updated my Mac today it wouldn’t let me reinstall lxml, I get the following error:
I have tried using brew to install libxml2 and libxslt, both installed fine but I still cannot install lxml.
Last time I was installing I needed to enable the developer tools on Xcode but since it’s updated to Xcode 5 it doesn’t give me that option anymore.
Does anyone know what I need to do?
23 Answers 23
You should install or upgrade the commandline tool for Xcode. Try this in a terminal:
I solved this issue on Yosemite by both installing and linking libxml2 and libxslt through brew:
If you have solved the problem using this method but it pops up again at a later time, you might need to run this before the four lines above:
If you are having permission errors with Homebrew, especially on El Capitan, this is a helpful document. In essence, regardless of OS X version, try running:
You may solve your problem by running this on the commandline:
It sure helped me. Explanations on docs
I tried most of the solutions above, but none of them worked for me. I’m running Yosemite 10.10, the only solution that worked for me was to type this in the terminal:
EDIT: If you are using virtualenv, the sudo in beginning is not needed.
This has been bothering me as well for a while. I don’t know the internals enough about python distutils etc, but the include path here is wrong. I made the following ugly hack to hold me over until the python lxml people can do the proper fix.
Installing globally. OS X 10.9.2
instalation instructions on http://lxml.de/installation.html explain:
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:
None of the above worked for me on 10.9.2, as compilation bails out with following error:
Which actually lead to cleanest solution (see more details in [1]):
or following if installing globally
I solved this issue on Yosemite by running the following commands:
With homebrew, libxml2 is hidden to not interfere with the system libxml2, so pip must be helped a little in order to find it.
I tried all the answers on this page, none of them worked for me. I’m running OS X Version 10.9.2
But this definitely works. like a charm:
ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install lxml
Unfortunately xcode-select —install did not work for me as I already had the latest version.
It’s very strange but I solved the issue by opening XCode and accepting the Terms & Conditions. Re-running pip install lxml returned no errors after.
After successful install from pip (lxml 3.6.4) I was getting an error when importing the lxml.etree module.
I was searching endlessly to install this as a requisite for scrapy, and tried all the options, but finally this worked for me (mac osx 10.11 python 2.7):
The older version of lxml seem to work with etree module.
Pip can often ignore the specified version of a package, for example when you have the newer version in the pip cache, thus the easy_install . The ‘-2.7’ option is for python version, omit this if you are installing for python 3.x.
In my case, I must shutdown Kaspersky Antivirus before installing lxml by:
before compiling add the path that to xmlversion.h into your environment.
But make sure the path I’ve provided has the xmlversion.h file located inside. Then,
pip did not work for me. I went to https://pypi.python.org/pypi/lxml/2.3 and downloaded the macosx .egg file:
Then used command line easy_install to install the .egg file.
I met the same question and after days of working I resolved this problem on my OS X 10.9.4, with Python 3.4.1.
Here’s my solution,
According to installing lxml from lxml.de,
A macport of lxml is available. Try something like port install py25-lxml
If you do not have MacPort, install it from MacPort.org. It’s quite easy. You may also need a compiler, to install Xcode compiling tools, use xcode-select —install
Firstly I updated my port to the latest version via sudo port selfupdate ,
Then I just type sudo port install libxml2 and several minutes later you should see libxml2 installed successfully. Probably you may also need libxslt to install lxml. To install libxslt, use: sudo port install libxslt .
Now, just type pip install lxml , it should work fine.
Источник