python3-ldap (Renamed to ldap3)
The `python3-ldap` project has been renamed to `ldap3`. This entry documents `python3-ldap` as a deprecated library and strongly advises users to migrate to `ldap3`, which is the actively maintained and current library for interacting with LDAP servers in Python. `ldap3` provides a comprehensive, modern, and high-performance feature set for LDAP client operations, including secure connections (LDAPS/StartTLS), asynchronous operations, and various authentication methods. The current version of `ldap3` is 2.9.1, and it maintains a steady release cadence.
Common errors
-
No module named 'ldap'
cause Attempting to import `ldap` (or `python3-ldap` modules) when only `ldap3` is installed, or after a partial migration.fixEnsure you are importing from `ldap3`. For example, replace `import ldap` or `from ldap import initialize` with `from ldap3 import Server, Connection`. -
AttributeError: 'module' object has no attribute 'initialize'
cause Trying to use an old `python-ldap` or `python3-ldap` API function (`ldap.initialize`) with the `ldap3` library.fixThe `ldap3` library uses `Server` and `Connection` classes. Replace `ldap.initialize(...)` with `server = Server(...)` and `conn = Connection(server, ...)`. -
`python3-ldap` is not a supported wheel on this platform.
cause The `python3-ldap` package is very old and often lacks pre-built wheels for modern Python versions or architectures, requiring a build from source which might fail.fixDo not attempt to install `python3-ldap`. Instead, use `pip install ldap3`. `ldap3` is actively maintained and provides wheels for current Python versions.
Warnings
- breaking The `python3-ldap` project is completely abandoned and has been renamed to `ldap3`. It receives no further updates, bug fixes, or security patches. Using `python3-ldap` is not recommended.
- breaking The API of `python3-ldap` is entirely incompatible with `ldap3`. Code written for `python3-ldap` will not work with `ldap3` without significant modification, as core classes, functions, and paradigms have changed.
- gotcha Installing `python3-ldap` might fail on modern Python versions or specific operating systems due to outdated dependencies or lack of wheel support.
Install
-
pip install python3-ldap -
pip install ldap3
Imports
- Server
from ldap.ldapobject import LDAPObject
from ldap3 import Server
- Connection
from ldap3 import Connection
- AUTH_SIMPLE
from ldap3 import AUTH_SIMPLE
- GET_DSE_INFO
from ldap3 import GET_DSE_INFO
Quickstart
import os
from ldap3 import Server, Connection, AUTH_SIMPLE, STRATEGY_SYNC, GET_DSE_INFO
# Configure LDAP connection details
LDAP_SERVER_IP = os.environ.get('LDAP_SERVER_IP', 'your_ldap_server.example.com')
LDAP_USER_DN = os.environ.get('LDAP_USER_DN', 'cn=admin,dc=example,dc=com') # e.g., 'uid=user,ou=users,dc=example,dc=com'
LDAP_PASSWORD = os.environ.get('LDAP_PASSWORD', 'admin_password')
LDAP_BASE_DN = os.environ.get('LDAP_BASE_DN', 'dc=example,dc=com')
try:
# Define the LDAP server
server = Server(LDAP_SERVER_IP, get_info=GET_DSE_INFO)
# Establish a connection
conn = Connection(server, user=LDAP_USER_DN, password=LDAP_PASSWORD, authentication=AUTH_SIMPLE)
# Bind to the server
if not conn.bind():
print(f"Error binding to LDAP: {conn.result}")
else:
print("Successfully bound to LDAP server.")
# Perform a search
search_filter = '(objectClass=person)' # Example filter
conn.search(LDAP_BASE_DN, search_filter, attributes=['cn', 'mail'])
print("\nSearch Results:")
for entry in conn.entries:
print(f" CN: {entry.cn}, Mail: {entry.mail}")
# Unbind from the server
conn.unbind()
print("Unbound from LDAP server.")
except Exception as e:
print(f"An error occurred during LDAP operation: {e}")