12

I have an python error AttributeError: 'module' object has no attribute 'initialize' I am running Python 2.6.2 on Solaris 10 UNIX and recently installed the pythonldap 2.3.9. The script is very basic, only has these 2 lines. Can anyone tell me why?? Traceback error below.

#!/usr/local/bin/python

import ldap, sys

con = ldap.initialize('ldap://localhost')

Traceback (most recent call last): File "./myldap.py", line 5, in con = ldap.initialize('ldap://localhost') AttributeError: 'module' object has no attribute 'initialize'

Regards, Jenny

Mike Graham
  • 73,987
  • 14
  • 101
  • 130
jenny
  • 121
  • 1
  • 1
  • 3
  • The preferred shebang line for Python is `!/usr/local/env python`, which runs whatever "python" would on the command line. – Mike Graham Mar 24 '10 at 04:25

7 Answers7

56

Did you name a file in the current directory ldap.py that is shadowing the one that you want?

Mike Graham
  • 73,987
  • 14
  • 101
  • 130
  • Impressive. Speedy and helpful replies in just less than 2 minutes! Yes I did actually, I've renamed that file now and got another error. Traceback (most recent call last): File "./myldap.py", line 3, in import ldap File "/usr/local/lib/python2.6/site-packages/ldap/__init__.py", line 22, in from _ldap import * ImportError: ld.so.1: python: fatal: libsasl2.so.2: open failed: No such file or directory – jenny Mar 24 '10 at 04:37
  • Bingo! Inspect the installation of your ldap module. Seems it may be horked. And welcome to SO!! – jathanism Mar 24 '10 at 04:38
  • It looks like you are having trouble with the C module `ldap` needs to call. What operating system do you use? How did you install python-ldap? – Mike Graham Mar 24 '10 at 04:39
  • Actually I didn't install this myself but the engineer who did forwarded me these details. Solaris 10. Python and Pythonldap were installed from Sunfreeware site - both packages specifically for Solaris systems. Python:- SMCpython 2.6.2 Pyhtonldap:- SMCpyldap 2.3.9 – jenny Mar 24 '10 at 05:31
  • @jenny: You should select this as an answer as it is the solution of your problem. It would help others to get their answer quickly. Thanks ! . It solved mine – Shashank Vivek Mar 03 '16 at 05:21
  • @MikeGraham that is some exceptional lateral thinking. I just fell into this one and feel quite sheepish right about now. Another thing though - remove the ldap.pyc too otherwise the error will persist... – mogoman Jul 19 '18 at 13:04
7

Many people are giving much more complicated solutions... Simply put, the pip installation of the ldap module doesn't work. You need to install the python-ldap package from apt or yum. Note that the deb package is now named python3-ldap, after the deprecation of python 2.

Arcsector
  • 1,153
  • 9
  • 14
4

An easy way to tell if the ldap you're importing is the right one is to print ldap.__file__, which prints the full path to the module file (usually a '.pyc'). If it's not the one installed in the location you are expecting, this is your problem, as Mike Graham suggested.

Community
  • 1
  • 1
jathanism
  • 33,067
  • 9
  • 68
  • 86
  • I made this mistake and this answer has helped me discover that even I had renamed my `ldap.py` to something else, there was still a `ldap.pyc` in the folder. – Xabs Nov 27 '14 at 13:16
3

I did the ldap connection successfully. How to go:

1.I have python v 3.7.2

2.Install python-ldap:For this I tried "pip install python-ldap" but it not worked for me on windows machine so I use the alternate below.

3.For installing ldap go here:https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap and download python_ldap‑3.1.0‑cp37‑cp37m‑win_amd64.whl

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

  1. Now open python shell and check "import ldap" if you are getting no error means you are done.

This is the 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))   
2

You can get that error if you're somehow picking up the "ldap.py" from sos/plugins/ instead of the ldap package itself. Make sure the "python-ldap" package is actually installed...

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
1

I guess you have installed "pip install ldap"! In this module "initialize" or "open" are not present. Uninstall that "ldap" by "pip uninstall ldap" and then try "yum install python-ldap". And run the same code. Print the "con".

Deepak
  • 19
  • 4
  • 1
    ldap isn't a pip installable module, you want python-ldap. Also @Arcsector has provided a more complete answer – Charles F Sep 21 '19 at 13:12
0

open does not exist anymore in version 3.x of python-ldap.

I fixed it by forcing the installation of an older version :

pip install python-ldap==2.4.13
fabiengb
  • 11
  • 1