Python ldap AttributeError
Asked Answered
I

7

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

Inferno answered 24/3, 2010 at 4:22 Comment(1)
The preferred shebang line for Python is !/usr/local/env python, which runs whatever "python" would on the command line.Lumbago
L
56

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

Lumbago answered 24/3, 2010 at 4:24 Comment(6)
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 <module> import ldap File "/usr/local/lib/python2.6/site-packages/ldap/__init__.py", line 22, in <module> from _ldap import * ImportError: ld.so.1: python: fatal: libsasl2.so.2: open failed: No such file or directoryInferno
Bingo! Inspect the installation of your ldap module. Seems it may be horked. And welcome to SO!!Modish
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?Lumbago
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.9Inferno
@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 mineCrissie
@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...Cruz
H
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.

Hoes answered 4/12, 2017 at 17:30 Comment(3)
this seems to be the case.Almagest
This is both true and horribleApoloniaapolune
this is not exactly true: "Changed in version 3.1: The deprecated functions ldap.init() and ldap.open() were removed." You are supposed to to ldap.initializeSaltpeter
M
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.

Modish answered 24/3, 2010 at 4:37 Comment(1)
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.Cannoneer
B
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 = '%[email protected]' % username
   # your password
   LDAP_PASSWORD = password
   base_dn = 'DC=spi,DC=com'
   ldap_filter = 'userPrincipalName=%[email protected]' % 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))   
Bradway answered 31/1, 2019 at 12:46 Comment(0)
L
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...

Lovemaking answered 24/3, 2010 at 4:49 Comment(0)
E
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".

Endear answered 3/5, 2018 at 7:19 Comment(1)
ldap isn't a pip installable module, you want python-ldap. Also @Hoes has provided a more complete answerSaltpeter
P
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
Photocurrent answered 7/10, 2020 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.