为什么我必须专门导入ldap.modlist?

时间:2016-10-28 18:54:59

标签: python python-2.7 ldap

我必须明确导入ldap.modlist有什么特别之处?导入ldap是不够的:

>>> import ldap
>>> ldap.modlist
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'modlist'

但如果我明确导入它,那么一切都很好:

>>> import ldap
>>> import ldap.modlist
>>> ldap.modlist
<module 'ldap.modlist' from '/opt/rh/python27/root/usr/lib64/python2.7/site-packages/ldap/modlist.pyc'>

为什么呢?其他模块不会表现得像这样:

>>> import os
>>> os.path
<module 'posixpath' from '/opt/rh/python27/root/usr/lib64/python2.7/posixpath.pyc'>

>>> import ldap
>>> ldap.dn
<module 'ldap.dn' from '/opt/rh/python27/root/usr/lib64/python2.7/site-packages/ldap/dn.pyc'>

1 个答案:

答案 0 :(得分:1)

模块ldap.dnldap.modlist之间的区别在于:

模块ldap.dnldap包初始化期间加载,因此已作为ldap包中的属性出现(有关说明,请参阅The Import System: Submodules子模块):

# ldap/__init__.py, line 89 of python-ldap version 2.4.27
from ldap.dn import explode_dn,explode_rdn,str2dn,dn2str

另一方面,在ldap.modlist包的初始化过程中未加载模块ldap