2

I've got a MacBook Pro running OS X 10.8, Xcode 4.4 installed. I recently installed lxml, but when I went to import it I got the following:

MacBook-Pro:~ jedc$ sudo pip install lxml==2.3.5
Password:
Downloading/unpacking lxml==2.3.5
  Downloading lxml-2.3.5.tar.gz (3.2Mb): 3.2Mb downloaded
  Running setup.py egg_info for package lxml
    Building lxml version 2.3.5.
    Building without Cython.
    Using build configuration of libxslt 1.1.26
    warning: no previously-included files found matching '*.py'
Installing collected packages: lxml
  Running setup.py install for lxml
    Building lxml version 2.3.5.
    Building without Cython.
    Using build configuration of libxslt 1.1.26
    building 'lxml.etree' extension
    /usr/bin/cc -fno-strict-aliasing -O3 -w -pipe -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/include/libxml2 -I/usr/local/Cellar/python/2.7.1/include/python2.7 -c src/lxml/lxml.etree.c -o build/temp.macosx-10.4-x86_64-2.7/src/lxml/lxml.etree.o -w -flat_namespace
    /usr/bin/cc -L/usr/local/Cellar/readline/6.1/lib -bundle -undefined dynamic_lookup -L/usr/local/Cellar/readline/6.1/lib build/temp.macosx-10.4-x86_64-2.7/src/lxml/lxml.etree.o -lxslt -lexslt -lxml2 -lz -lm -o build/lib.macosx-10.4-x86_64-2.7/lxml/etree.so
    building 'lxml.objectify' extension
    /usr/bin/cc -fno-strict-aliasing -O3 -w -pipe -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/include/libxml2 -I/usr/local/Cellar/python/2.7.1/include/python2.7 -c src/lxml/lxml.objectify.c -o build/temp.macosx-10.4-x86_64-2.7/src/lxml/lxml.objectify.o -w -flat_namespace
    /usr/bin/cc -L/usr/local/Cellar/readline/6.1/lib -bundle -undefined dynamic_lookup -L/usr/local/Cellar/readline/6.1/lib build/temp.macosx-10.4-x86_64-2.7/src/lxml/lxml.objectify.o -lxslt -lexslt -lxml2 -lz -lm -o build/lib.macosx-10.4-x86_64-2.7/lxml/objectify.so

Successfully installed lxml
Cleaning up...
MacBook-Pro:~ jedc$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named lxml

I've clearly got something incorrectly configured since lxml was successfully installed but Python can't find it. Here's other information:

MacBook-Pro:~ jedc$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
MacBook-Pro:~ jedc$ which pip
/usr/local/bin/pip
MacBook-Pro:~ jedc$ echo $PATH
/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/go/bin:/Users/jedc/.rvm/bin

And my ~/.bash_profile contains this:

PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

I've been advised that there's likely something I screwed up by (at some point previously) installing a non-Apple-default version of Python. Can anyone help me figure out what I need to do to diagnose this and then fix it?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Jed Christiansen
  • 659
  • 10
  • 21
  • I think you'll need to install Homebrew's version of Python and `pip`. – Blender Sep 19 '12 at 22:22
  • Given that you have at least two non-Apple versions of Python 2.7 installed, the first question is: do you need either or both for any reason? (Homebrew specifically recommends not using theirs unless you have to. Python.org suggests you might want theirs to stay more up-to-date than Apple, but 2.7.1 is the same one Apple's shipping, so you're not getting that benefit.) – abarnert Sep 19 '12 at 22:57

1 Answers1

6

Your pip command is installed for a different python (one installed in /usr/local/bin. You'll find that for /usr/local/bin/python the import works.

You need to install pip seperately for your other python (the one in /Library/Frameworks/Python.framework/Versions/2.7/bin/python) to have lxml installed for that python version.

Use /Library/Frameworks/Python.framework/Versions/2.7/pip to ensure the correct pip command is used to install lxml once pip has been installed.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Hmmm, just tried that and got the following: `Installing pip script to /Library/Frameworks/Python.framework/Versions/2.7/bin Installing pip-2.7 script to /Library/Frameworks/Python.framework/Versions/2.7/bin Successfully installed pip` then `pip install lxml Requirement already satisfied (use --upgrade to upgrade): lxml in /usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages Cleaning up...` and I still get `ImportError: No module named lxml` – Jed Christiansen Sep 19 '12 at 22:39
  • 1
    The system Python is *not* in `/Library/Frameworks/Python.framework/Versions/2.7/bin/python`. It's in `/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python`. From the version info shown, that appears to be a python.org python 2.7.2, not the Apple-supplied Python 2.7.2 in 10.8. – Ned Deily Sep 19 '12 at 22:41
  • So why is pip installing to `/usr/local/Cellar/python/2.7.1`? Do you also have a homebrew python installed? – Ned Deily Sep 19 '12 at 22:44
  • @JedChristiansen: your homebrew pip is still being used instead of the newly-installed one. Use the full path. – Martijn Pieters Sep 19 '12 at 22:46
  • @MartijnPieters Yes, I think I manually installed Python 2.7.2 about 6 months ago when I still was on OSX 10.6.8. I just followed the pip installation instructions you followed to get the output above. What do you mean by "use the full path?" – Jed Christiansen Sep 19 '12 at 22:50
  • 1
    @JedChristiansen: see my already updated answer. You could also just remove that whole installation (see [How to uninstall Python 2.7 on a Mac OS X 10.6.4?](http://stackoverflow.com/q/3819449) and just use the system python or the homebrew one (take your pick). – Martijn Pieters Sep 19 '12 at 22:53
  • 1
    I think the simplest thing here is to remove both the python.org and Homebrew pythons. Then the only copies of `easy_install` left will be the ones for your System python installs, and `sudo easy_install pip` will get you up and running. – abarnert Sep 19 '12 at 22:56
  • Success! Uninstalled Python.org and Homebrew versions. Did `sudo easy_install pip` and managed to reinstall lxml and successfully import it in the python console. Thanks so much to everyone! I'm fairly new to Stackoverflow but if I can do anything else to thank/give karma, please let me know. – Jed Christiansen Sep 19 '12 at 23:44