Python ldap 属性错误
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您是否在当前目录中命名了一个文件 ldap.py 来隐藏您想要的文件?
Did you name a file in the current directory ldap.py that is shadowing the one that you want?
许多人给出了更复杂的解决方案...简单地说,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 namedpython3-ldap
, after the deprecation of python 2.判断您导入的 ldap 是否正确的一种简单方法是打印 ldap.__file__ ,它会打印模块文件的完整路径(通常是 ' .pyc')。如果它不是安装在您期望的位置,那么这就是您的问题,正如 Mike Graham 建议的那样。
An easy way to tell if the
ldap
you're importing is the right one is to printldap.__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.我成功连接了 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”
这是示例代码:
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"
This is the sample code:
如果您以某种方式从 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...
我猜你已经安装了“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".
open 在 python-ldap 的 3.x 版本中不再存在。
我通过强制安装旧版本来修复它:
open does not exist anymore in version 3.x of python-ldap.
I fixed it by forcing the installation of an older version :