9

i have installed an lxml on my mac, when i type in python like this

localhost:lxml-3.0.1 apple$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
Traceback (most recent call last):
  File "", line 1, in 
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-3.0.1-py2.7-macosx-10.6-intel.egg/lxml/etree.so, 2): Symbol not found: ___xmlStructuredErrorContext
  Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-3.0.1-py2.7-macosx-10.6-intel.egg/lxml/etree.so
  Expected in: flat namespace
 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-3.0.1-py2.7-macosx-10.6-intel.egg/lxml/etree.so
Ray
  • 2,472
  • 18
  • 22
heghogbbb
  • 249
  • 1
  • 6
  • 13
  • 2
    This article may help: [Building Python Lxml in a Virtualenv on Mac OS X 10.7](http://roderickhodgson.com/blog/2012/10/27/building-python-lxml-on-mac-os-x-10-dot-7/) (you're probably linking against an older version of _libxml2_ than _lxml_ requires). – Pedro Romano Nov 13 '12 at 08:18
  • i will think about Virtualenv, but could i install lxml base on current python? – heghogbbb Nov 13 '12 at 10:24
  • thanks, i will return here after a test for this solution – heghogbbb Nov 13 '12 at 10:24
  • The same principle should apply to installing directly to your Python installation. Your problem is common to both scenarios. – Pedro Romano Nov 13 '12 at 10:32
  • after try a series lxml version 3.0.1, 2.3.5, 2.3.4, 2.3.3, 2.3.2, 2.3.1, get same error occured to each one, I must focos on your solution now, go go go ,thanks. – heghogbbb Nov 13 '12 at 10:54
  • 1
    Have you read this [section of the lxml documentation](http://lxml.de/installation.html#macos-x)? If you use [MacPorts](https://www.macports.org), the simplest solution is installing from [there](https://www.macports.org/ports.php?by=name&substr=lxml). Otherwise, your best bet is to follow the project's [instructions for Mac OS X](http://lxml.de/build.html#building-lxml-on-macos-x). – Pedro Romano Nov 13 '12 at 11:05
  • macports, and virtualenv are both okay, but i still want a solution on the python it selft, still can not omit this question, i will back soon, the installation of "STATIC_DEPS=true pip install --install-option="--libxml2-version=2.7.8" lxml-2.3.5.tgz " takes time, i need more fast internet connection later. – heghogbbb Nov 13 '12 at 11:21
  • STATIC_DEPS=true pip install lxml; cd build/lxml && python setup.py build --static-deps --libxml2-version=2.7.8 && pip install lxml, i faild at the command "cd build/lxml", where is the build directory? – heghogbbb Nov 13 '12 at 18:03
  • thanks, Pedro Romano , with your help , i have installed lxml finally, great you are:) – heghogbbb Nov 14 '12 at 03:29

4 Answers4

8

I had the same problem. If you have installed it with pip as follows: pip install lxml

Instead, try to use

STATIC_DEPS=true pip install lxml

This solved the problem for me.

Found at this website

Javaaaa
  • 3,788
  • 7
  • 43
  • 54
6

If you've installed libxml2, then it's possible that it's just not picking up the right version (there's a version installed with OS X by default). In particular, suppose you've installed libxml2 to /usr/local. You can check what shared libraries etree.so references:

$> otool -L /Library/Python/2.7/site-packages/lxml-3.2.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so 
/Library/Python/2.7/site-packages/lxml-3.2.1-py2.7-macosx-10.7-intel.egg/lxml/etree.so:
    /usr/lib/libxslt.1.dylib (compatibility version 3.0.0, current version 3.24.0)
    /usr/local/lib/libexslt.0.dylib (compatibility version 9.0.0, current version 9.17.0)
    /usr/lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.3.0)
    /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)

Checking for that symbol in the system-installed version:

$> nm /usr/lib/libxml2.2.dylib | grep ___xmlStructuredErrorContext

For me, it's not present in the system-installed library. In the version I installed, however:

$> nm /usr/local/lib/libxml2.2.dylib | grep ___xmlStructuredErrorContext
000000000007dec0 T ___xmlStructuredErrorContext

To solve this, make sure your install path appears first in DYLD_LIBRARY_PATH:

$> export DYLD_LIBRARY_PATH=/usr/local/lib
$> python
>>> from lxml import etree
# Success!
Dan Lecocq
  • 3,383
  • 25
  • 22
  • This solved my problem after an hour of trying... What is the permanent solution to this problem? – eaponte Jun 05 '15 at 11:48
  • You can probably add this path to `/etc/ld.so.conf.d/lxml.conf` (create a new file) and then run `ldconfig` (http://man7.org/linux/man-pages/man8/ldconfig.8.html) – Dan Lecocq Jun 05 '15 at 15:39
  • as for me, doing the opposite solved my problem (because the symbol was actually in the system's libxml2 version), so I had to put /usr/lib as the first entry of DYLD_LIBRARY_PATH – knocte Jul 06 '15 at 23:48
2

If you are having this problem in 2022 on an M1 Mac, try this:

pip3 uninstall lxml

pip3 install lxml

This will both upgrade lxml to the latest version (4.7.1 vs. 4.6.3 in my case) and clear files that could be causing issues per other anecdotal evidence on this problem.

Ben Wilson
  • 2,271
  • 3
  • 26
  • 35
  • try adding ```pip3 install lxml --no-cache-dir``` to force the re-download and building of wheels, that worked for me when initially the answer failed – Josheee Aug 22 '22 at 05:51
-1

Run the following command to install the lxml package.

pip install lxml --user 

should fix the issue. I tested it on MAC OSX 10.7.5, it worked fine.

Yin Song
  • 69
  • 1
  • 2