Pyldap (to use Django Auth LDAP) install in a pyth

2019-05-19 23:20发布

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.

2条回答
forever°为你锁心
2楼-- · 2019-05-20 00:04

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
查看更多
迷人小祖宗
3楼-- · 2019-05-20 00:15

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.

查看更多
登录 后发表回答