Python ldap AttributeError

I have a python error AttributeError object: 'module' does not have an 'initialize' attribute. I am running Python 2.6.2 on Solaris 10 UNIX and recently installed pythonldap 2.3.9. The script is very simple, has only two lines. Can someone tell me why? Trace error below.

#!/usr/local/bin/python import ldap, sys con = ldap.initialize('ldap://localhost') 

Traceback (last last call): File "./myldap.py", line 5, in con = ldap.initialize ('ldap: // localhost') AttributeError: the object 'module' does not have the attribute 'initialize'

Regards, Jenny

+9
python openldap
source share
6 answers

Did you name the file in the current ldap.py directory that obscures the one you need?

+30
source share

Many people offer much more complex solutions ... Simply put, installing the ldap module does not work. You need to install the python-ldap package from apt or yum.

+6
source share

An easy way to find out if the imported ldap correct: print ldap.__file__ , which prints the full path to the module file (usually this is ".pyc"). If this is not the one installed in the place you expect, this is your problem, as Mike Graham suggested.

+4
source share

You may get this error if you somehow compile "ldap.py" from sos / plugins / instead of the ldap package itself. Make sure the python-ldap package is actually installed ...

+2
source share

I suppose you installed "pip install ldap"! In this module, “initialize” or “open” is not. Uninstall this "ldap" with "pip uninstall ldap" and then try "yum install python-ldap". And run the same code. Print the cons.

+2
source share

I made the ldap connection successfully. How to get:

1. I have Python v 3.7.2

2. Install python-ldap: for this I tried pip install python-ldap , but it didn’t work for me on a Windows machine, so I use the alternative below.

3.To install ldap, follow the link: https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap and download python_ldap - 3.1.0 - cp37 - cp37m - win_amd64.whl

4. Now go to the download directory and run " pip install python_ldap - 3.1.0 - cp37 - cp37m - win_amd64.whl "

  1. Now open the Python shell and check "import ldap", if you do not get any errors, then everything is ready.

This is a sample code:

 #Resource of code :https://gist.github.com/ibeex/1288159 import ldap def check_credentials(username, password): """Verifies credentials for username and password. Returns None on success or a string describing the error on failure # Adapt to your needs """ LDAP_SERVER = 'xxx' # fully qualified AD user name LDAP_USERNAME = '% s@spi.com ' % username # your password LDAP_PASSWORD = password base_dn = 'DC=spi,DC=com' ldap_filter = 'userPrincipalName=% s@spi.com ' % username attrs = ['memberOf'] try: # build a client ldap_client = ldap.initialize(LDAP_SERVER) # perform a synchronous bind ldap_client.set_option(ldap.OPT_REFERRALS,0) ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD) except ldap.INVALID_CREDENTIALS: #print("wron") ldap_client.unbind() return 'Wrong username or password' except ldap.SERVER_DOWN: #print("down") return 'AD server not awailable' # all is well # get all user groups and store it in cerrypy session for future use ab = str(ldap_client.search_s(base_dn, ldap.SCOPE_SUBTREE, ldap_filter, attrs)[0][1]['memberOf']) #print("ab"+ab) ldap_client.unbind() return 'success' if __name__ == "__main__": u="chirag" p="secred" print(check_credentials(u,p)) 
0
source share

All Articles