Install & Compatibility
Where this runs
tested against v5.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 22.1MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 19MB
18MB installed
● package 18MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
connect
✓ cnxn = pyodbc.connect('DSN=your_dsn;UID=user;PWD=pass')
This quickstart demonstrates how to establish a connection to an ODBC database using `pyodbc`, execute SQL commands, insert data, and retrieve results. It uses environment variables for connection details and requires an appropriate ODBC driver and DSN (or DSN-less connection string) to be pre-configured on your system.
import pyodbc
import os
# NOTE: This example requires an ODBC driver and DSN to be configured on your system.
# For Windows, common drivers like 'SQL Server' might be available.
# For Linux/macOS, you might need to install unixODBC and specific database drivers.
# Example DSN-less connection string (replace with your actual details):
DB_DRIVER = os.environ.get('PYODBC_DRIVER', '{ODBC Driver 17 for SQL Server}')
DB_SERVER = os.environ.get('PYODBC_SERVER', 'your_server.database.windows.net')
DB_DATABASE = os.environ.get('PYODBC_DATABASE', 'your_database_name')
DB_UID = os.environ.get('PYODBC_UID', 'your_username')
DB_PWD = os.environ.get('PYODBC_PWD', 'your_password')
connection_string = (
f"DRIVER={DB_DRIVER};"
f"SERVER={DB_SERVER};"
f"DATABASE={DB_DATABASE};"
f"UID={DB_UID};"
f"PWD={DB_PWD};"
)
try:
cnxn = pyodbc.connect(connection_string)
cursor = cnxn.cursor()
# Example: Create a table (if it doesn't exist)
try:
cursor.execute("CREATE TABLE #TestTable (id INT, name VARCHAR(50))")
print("Table #TestTable created.")
except pyodbc.ProgrammingError as e:
if 'There is already an object named' in str(e): # For SQL Server temp table
print("Table #TestTable already exists, skipping creation.")
else:
raise
# Example: Insert data
cursor.execute("INSERT INTO #TestTable (id, name) VALUES (?, ?)", 1, 'Alice')
cursor.execute("INSERT INTO #TestTable (id, name) VALUES (?, ?)", 2, 'Bob')
cnxn.commit() # Commit changes if autocommit is not enabled
print("Data inserted.")
# Example: Select data
cursor.execute("SELECT id, name FROM #TestTable")
rows = cursor.fetchall()
print("\nFetched Data:")
for row in rows:
print(f"ID: {row.id}, Name: {row.name}")
except pyodbc.Error as ex:
sqlstate = ex.args[0]
print(f"Database Error (SQLSTATE: {sqlstate}): {ex}")
finally:
if 'cnxn' in locals() and cnxn:
cnxn.close()
print("\nConnection closed.")
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pyodbc'
The pyodbc library is not installed or is not available in the current Python environment.
pyodbc.OperationalError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
The specified ODBC driver is not installed on the system, is incorrectly named, or is not correctly configured.
fixInstall the appropriate ODBC driver for your database (e.g., ODBC Driver 17/18 for SQL Server) and ensure its name in the connection string is correct.
pyodbc.OperationalError: ('28000', '[28000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user \'your_username\'. (18456) (SQLDriverConnect)')
The provided username or password in the connection string is incorrect, or the user lacks the necessary database permissions.
fixVerify that the username and password in your connection string are correct and that the user has appropriate database access permissions.
AttributeError: 'NoneType' object has no attribute 'cursor'
The database connection failed to establish, resulting in a `None` connection object, and you are attempting to call the `.cursor()` method on it.
fixEnsure the `pyodbc.connect()` call succeeds by handling potential exceptions or verifying the connection object is not `None` before creating a cursor.
Upgrade
Version history
5.3.0latest on PyPI · released Oct 17, 2025
Audit
Dependencies
No dependency data recorded yet.