33

I can't seem to be able to get the python ldap module installed on my OS X Mavericks 10.9.1 machine.

Kernel details: uname -a Darwin 13.0.0 Darwin Kernel Version 13.0.0: Thu Sep 19 22:22:27 PDT 2013; root:xnu-2422.1.72~6/RELEASE_X86_64 x86_64

I tried what was suggested here: http://projects.skurfer.com/posts/2011/python_ldap_lion/

But when I try to use pip I get a different error

Modules/LDAPObject.c:18:10: fatal error: 'sasl.h' file not found

*#include sasl.h

I also tried what was suggested here: python-ldap OS X 10.6 and Python 2.6

But with the same error.

I am hoping someone could help me out here.

Community
  • 1
  • 1
gprx100
  • 370
  • 1
  • 4
  • 13

6 Answers6

96

using pieces from both @hharnisc and @mick-t answers.

pip install python-ldap \
   --global-option=build_ext \
   --global-option="-I$(xcrun --show-sdk-path)/usr/include/sasl"
dnozay
  • 23,846
  • 6
  • 82
  • 104
  • I don't have sudo permission on mac and this just did the trick ! – attaboyabhipro Oct 21 '14 at 03:20
  • This worked for me on Yosemite also. I didn't need to install anything macports or homebrew related for this particular install. Thanks! – etteyafed Oct 27 '14 at 04:33
  • 1
    This also works on El Capitan / 10.11 with System Integrity Protection on. – zakx Oct 15 '15 at 16:22
  • 2
    I was running into problems manually creating the link as in @hharnisc's solution (`ln: /usr/include: Operation not permitted`, even as sudo), but this worked on El Capitan. – Chris Vandevelde Oct 26 '15 at 22:00
  • to fix the issue about permissions and also isolate libraries installation, I suggest using virtualenv: "python -m virtualenv /yoursource; /yoursouhce/bin/pip install blah" this will install everything local to your project and avoid clashing with other projects. – Cross Jul 27 '17 at 09:52
  • That answer is still valid under Mojave 10.14.3 – Paraita Feb 22 '19 at 02:58
32

A workaround
/usr/include appears to have moved

$ xcrun --show-sdk-path    
$ sudo ln -s <the_path_from_above_command>/usr/include /usr/include

Now run pip install!

hharnisc
  • 937
  • 7
  • 11
24

In my particular case, I couldn't simply use the pip arguments noted in other answers because I'm using it with tox to install dependencies from a requirements.txt file, and I need my tox.ini to remain compatible with non-Mac environments.

I was able to resolve this in much simpler fashion: exporting CFLAGS such that it adds an include path to the sasl headers already installed by Xcode:

$ pip install python-ldap
    ...
    building '_ldap' extension
    creating build/temp.macosx-10.10-x86_64-2.7
    creating build/temp.macosx-10.10-x86_64-2.7/Modules
    clang -fno-strict-aliasing -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DHAVE_SASL -DHAVE_TLS -DHAVE_LIBLDAP_R -DHAVE_LIBLDAP_R -DLDAPMODULE_VERSION=2.4.19 -IModules -I/opt/openldap-RE24/include -I/usr/include/sasl -I/usr/include -I/Users/bc/.pyenv/versions/2.7.10/include/python2.7 -c Modules/LDAPObject.c -o build/temp.macosx-10.10-x86_64-2.7/Modules/LDAPObject.o
    Modules/LDAPObject.c:18:10: fatal error: 'sasl.h' file not found
    #include <sasl.h>
             ^
    1 error generated.
    error: command 'clang' failed with exit status 1

$ export CFLAGS="-I$(xcrun --show-sdk-path)/usr/include/sasl"

$ pip install python-ldap
...
Successfully installed python-ldap-2.4.19

Depending on whether or not you use any userspace-friendly Python tools (I use pyenv), you may have to prefix your pip commands with sudo.

Brian Cline
  • 20,012
  • 6
  • 26
  • 25
5

I had the same problem. I'm using Macports on my Mac and I have cyrus-sasl2 installed which provides sasl.h in /opt/local/include/sasl/. You can pass options to build_ext using pip's global-option argument. To pass the include PATH to /opt/local/include/sasl/sasl.h run pip like this:

pip install python-ldap --global-option=build_ext --global-option="-I/opt/local/include/sasl"

Alternatively you could point it to whatever the output from xcrun --show-sdk-path provides. On my box that's: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk

Then you need to determine the PATH to the sasl header files. For me that's:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/sasl/

Let me know if that helps or you need a hand.

Mick T
  • 377
  • 4
  • 10
  • sudo pip install python-ldap --global-option=build_ext --global-option="-I$(xcrun --show-sdk-path)/usr/include/sasl" Downloading/unpacking python-ldap Error while getting https://pypi.python.org/packages/source/p/python-ldap/python-ldap-2.4.19.tar.gz#md5=b941bf31d09739492aa19ef679e94ae3 (from https://pypi.python.org/simple/python-ldap/) – gprx100 Apr 08 '15 at 17:27
1

I used a combination of posts I found about this problem (including this one) to eventually come up with this (copied from a larger script):

export XC_SDK=$(xcrun --show-sdk-path)
export USR_INC=$XC_SDK/usr/include
export PATH=$USR_INC:$PATH

echo "installing python-ldap"
ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install python-ldap

You can test it with python -c "import ldap"

The main reason I didn't follow the advice of @hharnisc was that on my local machine /usr/local had not moved, so I just temporarily put $XC_SDK before it on the path, and that seems to work.

some sources:
how to install PIL on Macosx 10.9?

Community
  • 1
  • 1
Peter Hanley
  • 1,254
  • 1
  • 11
  • 19
0

I got this error when running buildout.

I fixed it, first finding the sasl.h file:

mdfind -name sasl.h

then defining the corresponding CFLAGS environment variable:

export CFLAGS="-I/opt/local/include/sasl"

and finally running buildout again.

erny
  • 1,296
  • 1
  • 13
  • 28