"Fix: (IM002) [Microsoft][ODBC Driver Manager] Data Source Name Not Found"

Solving the QODBC3 connection error when using QSqlDatabase in PyQt5
Heads up! You've already completed this tutorial.

I'm trying to connect to a database using QSqlDatabase with the QODBC3 driver in PyQt5, but I keep getting the error: (IM002) [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified. How do I fix this?

If you're using PyQt5's QSqlDatabase with the QODBC3 driver and you see this error, the good news is that the ODBC driver is working — PyQt5 is successfully talking to the Microsoft ODBC Driver Manager. The problem is that the Driver Manager doesn't know which database you're trying to connect to, because no matching ODBC Data Source Name (DSN) has been configured on your system, or the connection string you're using doesn't include enough information for the driver to find your database directly.

Let's walk through what's happening and how to fix it.

Understanding the error

When you use QODBC3 in PyQt5, Qt delegates the actual database connection to the Windows ODBC Driver Manager. The Driver Manager then looks up the name you passed to setDatabaseName() in its list of configured data sources. If it can't find a match — and the string you provided isn't a full connection string with a driver specified — you get the IM002 error.

In the original code, the connection looks like this:

python
db = QtSql.QSqlDatabase.addDatabase('QODBC3')
db.setHostName('xxx')
db.setDatabaseName('xxx')
db.setUserName("xxx")
db.setPassword('xxxx')
ok = db.open()

The value passed to setDatabaseName() is what the ODBC Driver Manager uses to find your database. If that value isn't a registered DSN or a complete ODBC connection string, the connection will fail.

There are two ways to fix this: configure a DSN in Windows, or use a DSN-less connection string.

Option 1: Set up an ODBC Data Source (DSN)

The most common fix is to register your database as a data source in the Windows ODBC Data Source Administrator. This tells the ODBC Driver Manager how to reach your database when you refer to it by name.

Here's how to do it:

  1. Open the Start menu and search for "ODBC Data Sources". You'll see two versions: one for 32-bit and one for 64-bit. Choose the one that matches your Python installation (most likely 64-bit).

    Over 15,000 developers have bought Create GUI Applications with Python & Qt!
    Create GUI Applications with Python & Qt6
    Get the book

    Downloadable ebook (PDF, ePub) & Complete Source code

  2. In the ODBC Data Source Administrator window, click the "Add..." button on the User DSN or System DSN tab.

  3. Select the appropriate driver for your database (for example, "SQL Server", "SQL Server Native Client", "ODBC Driver 17 for SQL Server", or a MySQL/PostgreSQL ODBC driver — depending on what database you're connecting to).

  4. Follow the wizard to configure the connection, providing your server name, database name, and authentication details.

  5. Give the data source a name — for example, MyDatabase.

  6. Click Finish to save.

Now, back in your PyQt5 code, use that DSN name as the database name:

python
import sys
from PyQt5 import QtSql
from PyQt5.QtSql import QSqlQuery
from PyQt5.QtWidgets import QApplication


def dbcon():
    db = QtSql.QSqlDatabase.addDatabase('QODBC3')
    db.setDatabaseName('MyDatabase')  # The DSN name you configured
    db.setUserName("your_username")
    db.setPassword("your_password")
    ok = db.open()
    if not ok:
        print(db.lastError().text())
    else:
        print("Connected successfully!")
        query = QSqlQuery(db)
        query.exec_('SELECT * FROM IMSS')
        while query.next():
            print(query.value(0))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dbcon()

Notice that setHostName() isn't needed when using a DSN — the host information is already stored in the data source configuration.

Option 2: Use a DSN-less connection string

If you don't want to configure a DSN in Windows, you can pass a full ODBC connection string directly to setDatabaseName(). This approach includes the driver name and all connection details inline, so the ODBC Driver Manager doesn't need to look anything up.

For example, to connect to a SQL Server database:

python
import sys
from PyQt5 import QtSql
from PyQt5.QtSql import QSqlQuery
from PyQt5.QtWidgets import QApplication


def dbcon():
    db = QtSql.QSqlDatabase.addDatabase('QODBC3')
    connection_string = (
        "DRIVER={SQL Server};"
        "SERVER=your_server_name;"
        "DATABASE=your_database_name;"
        "UID=your_username;"
        "PWD=your_password;"
    )
    db.setDatabaseName(connection_string)
    ok = db.open()
    if not ok:
        print(db.lastError().text())
    else:
        print("Connected successfully!")
        query = QSqlQuery(db)
        query.exec_('SELECT * FROM IMSS')
        while query.next():
            print(query.value(0))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    dbcon()

The DRIVER= part of the connection string tells the ODBC Driver Manager exactly which driver to use, bypassing the DSN lookup entirely. The driver name must match one that's installed on your system. Common driver names include:

Database Driver name
SQL Server (older) {SQL Server}
SQL Server (newer) {ODBC Driver 17 for SQL Server} or {ODBC Driver 18 for SQL Server}
MySQL {MySQL ODBC 8.0 Unicode Driver}
PostgreSQL {PostgreSQL Unicode}
Microsoft Access {Microsoft Access Driver (*.mdb, *.accdb)}

To see which ODBC drivers are installed on your system, open the ODBC Data Source Administrator and click on the Drivers tab.

Checking which drivers are available

If you're not sure which ODBC drivers are installed, you can also check from Python using the pyodbc library (install it with pip install pyodbc if needed):

python
import pyodbc

for driver in pyodbc.drivers():
    print(driver)

This prints a list of all available ODBC drivers on your machine. Use the exact name from this list (wrapped in curly braces) in your connection string.

Common pitfalls

32-bit vs 64-bit mismatch. If you're running 64-bit Python but your ODBC driver (or DSN) is only registered for 32-bit, the Driver Manager won't find it. Make sure your Python installation and your ODBC driver are both the same architecture. You can check this by running python -c "import struct; print(struct.calcsize('P') * 8)" to see whether your Python is 32-bit or 64-bit.

Missing ODBC driver. If the database driver isn't installed at all, you'll need to download and install it. For SQL Server, Microsoft provides the ODBC Driver for SQL Server as a free download.

Typo in the DSN or driver name. The names are case-sensitive and must match exactly what's registered in the ODBC Data Source Administrator. Double-check the spelling.

Summary

The IM002 error means the ODBC Driver Manager can't find the data source you're referring to. You can fix it by either registering a DSN in the Windows ODBC Data Source Administrator and using that name in setDatabaseName(), or by passing a complete DSN-less connection string that includes the DRIVER= parameter. Both approaches work well — the DSN-less connection string is often more portable since it doesn't depend on per-machine ODBC configuration.

Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

Get the book

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak
Martin Fitzpatrick

"Fix: (IM002) [Microsoft][ODBC Driver Manager] Data Source Name Not Found" was written by Martin Fitzpatrick.

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular Python books on the subject.