11

我有一个 python 错误AttributeError: 'module' object has no attribute 'initialize' 我在 Solaris 10 UNIX 上运行 Python 2.6.2,最近安装了 pythonldap 2.3.9。脚本很基础,只有这两行。谁能告诉我为什么??回溯错误如下。

#!/usr/local/bin/python

import ldap, sys

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

回溯(最后一次调用):文件“./myldap.py”,第 5 行,在 con = ldap.initialize('ldap://localhost') AttributeError: 'module' object has no attribute 'initialize'

问候,珍妮

4

7 回答 7

47

您是否在当前目录 ldap.py 中命名了一个文件,该文件隐藏了您想要的文件?

于 2010-03-24T04:24:55.610 回答
7

很多人都给出了更复杂的解决方案...简单地说,ldap模块的pip安装不起作用。您需要从 apt 或 yum 安装 python-ldap 包。python3-ldap请注意,在弃用 python 2 之后,deb 包现在被命名为。

于 2017-12-04T17:30:13.473 回答
4

判断ldap您正在导入的是否正确的一种简单方法是 print ldap.__file__,它会打印模块文件的完整路径(通常是“.pyc”)。如果它不是安装在您期望的位置的那个,这就是您的问题,正如Mike Graham所建议的那样。

于 2010-03-24T04:37:18.860 回答
3

我成功地完成了 ldap 连接。怎么去:

1.我有python v 3.7.2

2.安装python-ldap:为此我尝试了“ pip install python-ldap ”,但它在Windows机器上对我不起作用,所以我使用下面的替代方法。

3.安装ldap去这里:https ://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap 并下载python_ldap‑3.1.0‑cp37‑cp37m‑win_amd64.whl

4.现在移动到下载目录并运行“ pip install python_ldap‑3.1.0‑cp37‑cp37m‑win_amd64.whl

  1. 现在打开 python shell 并检查“import ldap”如果你没有收到错误意味着你已经完成了。

这是示例代码:

#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))   
于 2019-01-31T12:46:04.187 回答
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...

于 2010-03-24T04:49:46.147 回答
1

我猜你已经安装了“pip install ldap”!在此模块中“初始化”或“打开”不存在。通过“pip uninstall ldap”卸载该“ldap”,然后尝试“yum install python-ldap”。并运行相同的代码。打印“骗局”。

于 2018-05-03T07:19:36.570 回答
0

在 python-ldap 的 3.x 版本中,open 不再存在。

我通过强制安装旧版本来修复它:

pip install python-ldap==2.4.13
于 2020-10-07T13:35:02.153 回答