技术咨询:为何无法从ldap3库导入LDAPBindError?
Hey there, I’ve run into this exact issue before with ldap3’s version shifts—let’s break down why this is happening and how to fix it quickly.
Why the Error Occurs
In ldap3 version 2.x (including your 2.5 release), the LDAPBindError exception is no longer exposed directly under the top-level ldap3 module. This is a breaking change from older ldap3 1.x versions, where you could import it straight from the root module.
Instead, all ldap3-specific exceptions (including LDAPBindError) are now organized into the ldap3.core.exceptions submodule. Your current import statement is trying to pull it from the root module, which no longer houses it—hence the ImportError.
How to Fix It
You just need to adjust your import line to fetch LDAPBindError from its new dedicated location:
# Keep your existing imports from the root module from ldap3 import Server, Connection, ALL, SUBTREE, ALL_ATTRIBUTES, MODIFY_REPLACE # Add this line to import the bind error from the correct submodule from ldap3.core.exceptions import LDAPBindError
To verify this works, you can test it in a Python shell:
import ldap3 # Check that LDAPBindError isn't in the root module's namespace print("LDAPBindError" in dir(ldap3)) # Will output False # Import from the correct location from ldap3.core.exceptions import LDAPBindError print(LDAPBindError) # Will output <class 'ldap3.core.exceptions.LDAPBindError'> if successful
This change aligns with ldap3’s 2.x restructuring of exception classes into a dedicated module for cleaner code organization.
内容的提问来源于stack exchange,提问作者user1768233

