3

First, I installed lxml without using pip (Python 2.7.2 on Mac OS 10.6.8). Then, I read this post and I installed it again using pip (sudo pip install lxml). I still had a problem:

I can import lxml (import lxml) but I cannot use from lxml import etree. I have this error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so, 2): Symbol not found: _htmlParseChunk
  Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-2.3.3-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-2.3.3-py2.7-macosx-10.6-intel.egg/lxml/etree.so

Then, I tried to install lxml from source following these instructions, and I have this error message:

checking whether we are cross compiling... configure: error: in `/Users/my_name/Applications/lxml/lxml-2.2.2/build/tmp/libxml2-2.7.8':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
See `config.log' for more details
Traceback (most recent call last):
  File "setup.py", line 115, in <module>
    STATIC_CFLAGS, STATIC_BINARIES),
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/setupinfo.py", line 50, in ext_modules
    libxslt_version=OPTION_LIBXSLT_VERSION)
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/buildlibxml.py", line 198, in build_libxml2xslt
    call_subprocess(configure_cmd, cwd=libxml2_dir, **call_setup)
  File "/Users/my_name/Applications/lxml/lxml-2.2.2/buildlibxml.py", line 158, in call_subprocess
    raise Exception('Command "%s" returned code %s' % (cmd_desc, returncode))
Exception: Command "./configure --without-python --disable-dependency-tracking --disable-shared --prefix=/Users/my_name/Applications/lxml/lxml-2.2.2/build/tmp/libxml2" returned code 1

Finally, I followed the second advise of this answer and I used the command line sudo STATIC_DEPS=true /usr/bin/easy_install-2.7 lxml. It installed lxml on the Apple-supplied system Python 2.7, and not on the version I'm currently using. The positive point: if I run the Apple-Python, I can from lxml import etree.

Negative point: I still don't know how to install lxml on another version of python. Any idea?

I'm currently using /Library/Frameworks/Python.framework/Versions/2.7/bin/python.

Community
  • 1
  • 1
Antonin
  • 1,748
  • 7
  • 19
  • 24

1 Answers1

4

You need to install a separate easy_install for the version of Python you are using. See the answer here for more details. Then you can run the easy_install command using it:

STATIC_DEPS=true easy_install-2.7 lxml

UPDATE: From your comments, you now are reporting a permission error showing yet another Python path, one that appears to be a MacPorts-installed Python: /opt/local/Library/Frameworks. You need to figure out which Python you want to use. If, in fact, you want to use the MacPorts one, then simply install the MacPorts-provided lxml port. That's the easiest solution.

$ sudo port selfupdate
$ sudo port install py27-lxml

Otherwise, you need to install easy_install with the other (python.org?) Python 2.7.2. I would recommend to use Distribute, the more modern fork of `setuptools' and you need to ensure you are using the right Python:

$ curl -O http://python-distribute.org/distribute_setup.py
$ export PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
$ which python2.7   # should be in the path above
$ python2.7 distribute_setup.py
$ STATIC_DEPS=true easy_install-2.7 lxml
Community
  • 1
  • 1
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • So first I installed easy_install using `sh setuptools-0.6c11-py2.7.egg` (after downloading the egg [here](http://pypi.python.org/pypi/setuptools#downloads), then I modified my path (`export PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"`), and finally I installed lxml (`STATIC_DEPS=true easy_install-2.7 lxml`). Still have a problem with this last command: `error: can't create or remove files in install directory` – Antonin Mar 25 '12 at 10:11
  • `The following error occurred while trying to add or remove files in the installation directory: [Errno 13] Permission denied: '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/test-easy-install-1180.write-test'}` – Antonin Mar 25 '12 at 10:15
  • By the way, what's the difference with `STATIC_DEPS=true easy_install lxml`? (without -2.7) – Antonin Mar 25 '12 at 10:49
  • See the updated answer. Distribute and setuptools install both unversioned (`easy_install`) and versioned (`easy_install-2.7`) copies of the command. Within a particular Python instance, they should produce the same results. – Ned Deily Mar 25 '12 at 19:12
  • @Antonin: the added `-2.7` is referring to a specific version of easy_install since you have multiple pythons installed. I actually wanna throw this out there if you are using macports...I recommend completely removing it and going with Homebrew. – jdi Mar 25 '12 at 19:13
  • Thank you very much for this very complete answer, very helpful. Unfortunately, I tried to install lxml with MacPorts on the correct version of Python, and I still have the same error message when importing etree from lxml. – Antonin Mar 25 '12 at 20:14
  • When using `STATIC_DEPS=true easy_install-2.7 lxml`, I have: `Searching for lxml Best match: lxml 2.3.3 Processing lxml-2.3.3-py2.7-macosx-10.6-intel.egg lxml 2.3.3 is already the active version in easy-install.pth` – Antonin Mar 25 '12 at 20:15
  • And when `python2.7 distribute_setup.py`, I have: `/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info already exists` (at the very end) – Antonin Mar 25 '12 at 20:18
  • But still, when `which python2.7`-ing, I have `/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7` – Antonin Mar 25 '12 at 20:19
  • I have too many versions of python on my machine I guess, and all of them installed differently. I should clean all this mess, but I'm afraid to have even more trouble then. – Antonin Mar 25 '12 at 20:20
  • @jdi: Throwing MacPorts out and adding Homwbrew to the mix here is not a very useful suggestion. The problem is not MacPorts here, it's confusion over which of the three different Pythons the OP has reported using is actually being used to install lxml. @Antonin: Pick one Python and stick with it. It's really hard to tell what's going on with so little information. Use `which easy_install` to ensure you are using the one you intend to use. Then use `easy_install -m lxml` and remove the egg files/directories reported and then try installing again. Or just use MacPorts as suggested. – Ned Deily Mar 25 '12 at 20:29
  • @Antonin: If you don't mind losing any site packages you've already installed, you can just go into `/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/` and manually delete the files and directories there, then start again with installing Distribute and then `lxml`. You might also want to check and, if necessary, edit your shell profile file (most likely `$HOME/.bash_profile`) to ensure that the expected Python framework bin directory comes first on your PATH. Good luck! – Ned Deily Mar 25 '12 at 20:33