Python ldap 属性错误

发布于 2024-08-26 11:40:26 字数 447 浏览 12 评论 0原文

我有一个 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:“模块”对象没有属性“初始化”

问候, 珍妮

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

离不开的别离 2024-09-02 11:40:26

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

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

追星践月 2024-09-02 11:40:26

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

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.

甜扑 2024-09-02 11:40:26

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

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.

歌枕肩 2024-09-02 11:40:26

我成功连接了 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 = '%[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))   

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))   
GRAY°灰色天空 2024-09-02 11:40:26

如果您以某种方式从 sos/plugins/ 中获取“ldap.py”而不是 ldap 包本身,则可能会出现该错误。确保“python-ldap”软件包已实际安装...

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...

烟雨凡馨 2024-09-02 11:40:26

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

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".

空袭的梦i 2024-09-02 11:40:26

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

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

pip install python-ldap==2.4.13

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文