2

I am trying to install pyldap to use it with django-auth-ldap but when doing:

 pip install pyldap

I get:

 In file included from Modules/LDAPObject.c:4:0:
 Modules/common.h:10:20: fatal error: Python.h: No such file or directory
 #include "Python.h"
                    ^
compilation terminated.
error: command 'gcc' failed with exit status 1

Important

I am using Virtualenv, witch was created in python3:

virtualenv -p python3 myvirtualenv

And I have already installed the development packages (I am in CentOS, found in this answer) :

sudo yum install python-devel
sudo yum install openldap-devel

What I already tried

Create two new virtualenv:

  • One with python 3.4 (using the command above) and the pip install pyldapdoesn't work (with the error described above)
  • One with python 2.7 (using the command above without -p python3)and the pip install pyldap works
  • Install the package as root sudo pip3 install pyldapand it works but when I run in the django app in the virtualenv I get:

    import ldap
    ImportError: No module named 'ldap'
    

I pretend to use pyldap precisely because it supports python3 so I can't understand why this happens and how may I resolve it. If you can't answer this question directly but you know other library I may use for Active Directory authentication in Django please comment this question.

Community
  • 1
  • 1
NBajanca
  • 3,544
  • 3
  • 26
  • 53
  • I don't know if this is a general question for virtualenv or if it is related to something more specific so please guide me so I may edit the question and make it more understandable. – NBajanca Apr 27 '16 at 15:31
  • What does `pip freeze | grep ldap` show? – e4c5 Apr 27 '16 at 15:38
  • Doesn't show anything – NBajanca Apr 27 '16 at 15:41
  • 1
    that means pyldap has not been install OR you have not done `source myvirtualenv/bin/activate` – e4c5 Apr 27 '16 at 15:42
  • I have virtualenv activated and pip freeze shows other packages I have installed. When I installed pyldap with sudo and I got a success message (installing again it says the only option is upgrade, witch I've tried and does nothing) – NBajanca Apr 27 '16 at 15:47

2 Answers2

3

you cannot install a package into a virtualenv using sudo. You must use sudo only to install the dependencies. For example

sudo apt-get install -y python-dev libldap2-dev libsasl2-dev libssl-dev

updated: However if your system's default version of python is 2.x and your virtualenv is 3.x you will actually need to install python3-dev instead of python-dev.

For the actual installation

source myvirtualenv/bin/activate
pip3 install pyldap

You are using pip3 here so make sure that your virtualenv has been setup to use python 3 with a command such as the following:

virtualenv -p python3 myvirtualenv

You can find out if what version is actually being used by entering the python shell.

As a foot note, if you really want to install a package as root:

sudo -i
source myvirtualenv/bin/activate
pip3 install pyldap
e4c5
  • 52,766
  • 11
  • 101
  • 134
  • Thanks for the answer. I had already done that but my question wasn't clear enough (I already edited it, please check it again). I don't really want to install as root, I just want to install it and I am not able to – NBajanca Apr 28 '16 at 15:42
  • Are you still getting the missing python.h error? if not how about leaving out all that information including the installation of devel-packages. Also how about trying the install with the --verbose option of pip? – e4c5 Apr 28 '16 at 16:07
  • Same error, nothing changed. I added pip install --verbose to the question. – NBajanca Apr 28 '16 at 16:27
  • 1
    Updated answer. I think what you have is python.h for 2.xx you need to install python3-dev as well. – e4c5 Apr 28 '16 at 16:33
  • Successfully installed pyldap-2.4.25.1!! Thank you. The resolution was exactly that but CentOS 7 doesn't have the devel package so I needed to install the EPEL repository. – NBajanca May 04 '16 at 15:51
  • Please simplify the answer in relation to my question so it is easier for others to understand. The resolution was just installing the python3-dev – NBajanca May 04 '16 at 15:55
0

You have to install python-dev package (assuming you use a Ubuntu/Debian Linux)

sudo apt-get install python-dev

Then try to install pyldap using pip3 without sudo.

Pancho Jay
  • 490
  • 5
  • 10