John Gordon John Gordon - 1 year ago 94
Python Question

Why do I have to specifically import ldap.modlist?

What is so special about

that I have to explicitly import it? Importing
is not enough:

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

But if I explicitly import it, then all is well:

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

Why? Other modules don't behave like this:

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

Answer Source

The difference between the modules ldap.dn and ldap.modlist is this:

The module ldap.dn is loaded during the initialization of the ldap package and is therefore already present as an attribute in the ldap package (see The Import System: Submodules for a description about submodules):

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

The module ldap.modlist on the other hand is not loaded during the initalization of the ldap package.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download