<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Python GUIs - mysql</title><link href="https://www.pythonguis.com/" rel="alternate"/><link href="https://www.pythonguis.com/feeds/mysql.tag.atom.xml" rel="self"/><id>https://www.pythonguis.com/</id><updated>2020-08-19T09:00:00+00:00</updated><subtitle>Create GUI applications with Python and Qt</subtitle><entry><title>PyQt5 MYSQL driver not loaded on Windows — How to fix the "QMYSQL driver not loaded" error when connecting to MySQL from PyQt5</title><link href="https://www.pythonguis.com/faq/pyqt5-mysql-driver-not-loaded-windows/" rel="alternate"/><published>2020-08-19T09:00:00+00:00</published><updated>2020-08-19T09:00:00+00:00</updated><author><name>Martin Fitzpatrick</name></author><id>tag:www.pythonguis.com,2020-08-19:/faq/pyqt5-mysql-driver-not-loaded-windows/</id><summary type="html">The &lt;code&gt;QMYSQL driver not loaded&lt;/code&gt; error is one of the most common stumbling blocks when trying to use PyQt5 with a MySQL database on Windows. You'll typically see something like this when running your script:</summary><content type="html">
            &lt;p&gt;The &lt;code&gt;QMYSQL driver not loaded&lt;/code&gt; error is one of the most common stumbling blocks when trying to use PyQt5 with a MySQL database on Windows. You'll typically see something like this when running your script:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This is confusing because the output &lt;em&gt;lists&lt;/em&gt; &lt;code&gt;QMYSQL&lt;/code&gt; as an available driver -- so why can't it load? The answer comes down to a missing dependency: the MySQL client library DLL. The Qt MySQL plugin (&lt;code&gt;qsqlmysql.dll&lt;/code&gt;) is a thin wrapper that relies on the MySQL C Connector library to actually talk to the database. If that library isn't where Qt can find it, the plugin fails to load.&lt;/p&gt;
&lt;p&gt;Let's walk through how to fix this.&lt;/p&gt;
&lt;h2 id="understanding-the-problem"&gt;Understanding the Problem&lt;/h2&gt;
&lt;p&gt;Qt's SQL system works with plugins. When you call &lt;code&gt;QSqlDatabase.addDatabase('QMYSQL')&lt;/code&gt;, Qt looks for a plugin file (on Windows, &lt;code&gt;qsqlmysql.dll&lt;/code&gt;) in its &lt;code&gt;sqldrivers&lt;/code&gt; plugin directory. That plugin file &lt;em&gt;itself&lt;/em&gt; depends on the MySQL C client library -- a separate DLL provided by MySQL, not by Qt or PyQt5.&lt;/p&gt;
&lt;p&gt;If Qt finds the plugin file but can't load it (because the MySQL client library is missing or can't be found), you get the "driver not loaded" error. The driver is &lt;em&gt;listed&lt;/em&gt; as available because the plugin file exists, but it can't actually be &lt;em&gt;used&lt;/em&gt; because its dependency is missing.&lt;/p&gt;
&lt;h2 id="finding-your-pyqt5-plugin-directory"&gt;Finding Your PyQt5 Plugin Directory&lt;/h2&gt;
&lt;p&gt;First, let's confirm where your PyQt5 installation keeps its SQL driver plugins. Open a Python prompt and run:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import os
from PyQt5.QtCore import QLibraryInfo

plugin_path = QLibraryInfo.location(QLibraryInfo.PluginsPath)
sqldrivers_path = os.path.join(plugin_path, "sqldrivers")
print("Plugin path:", plugin_path)
print("SQL drivers path:", sqldrivers_path)
print("Files:", os.listdir(sqldrivers_path))
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You should see &lt;code&gt;qsqlmysql.dll&lt;/code&gt; (or &lt;code&gt;qsqlmysqld.dll&lt;/code&gt; for debug builds) listed among the files. If it's there, your Qt installation already has the MySQL plugin -- it just can't load it because of the missing dependency.&lt;/p&gt;
&lt;h2 id="getting-the-mysql-client-library"&gt;Getting the MySQL Client Library&lt;/h2&gt;
&lt;p&gt;You need the MySQL C Connector client library DLL. The specific file you need is called &lt;code&gt;libmysql.dll&lt;/code&gt;. There are two ways to get it.&lt;/p&gt;
&lt;h3&gt;Option 1: From an Existing MySQL Installation&lt;/h3&gt;
&lt;p&gt;If you already have MySQL 8 installed on your machine, &lt;code&gt;libmysql.dll&lt;/code&gt; is probably already on your system. Look in your MySQL installation directory, typically something like:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;C:\Program Files\MySQL\MySQL Server 8.0\lib\
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You should find &lt;code&gt;libmysql.dll&lt;/code&gt; in that &lt;code&gt;lib&lt;/code&gt; folder.&lt;/p&gt;
&lt;h3&gt;Option 2: Download the MySQL C Connector&lt;/h3&gt;
&lt;p&gt;If you don't have a full MySQL server installed locally (for example, you're connecting to a remote database), you can download just the connector:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://dev.mysql.com/downloads/connector/c/"&gt;MySQL Community Downloads&lt;/a&gt; page (or search for "MySQL Connector/C").&lt;/li&gt;
&lt;li&gt;Download the &lt;strong&gt;Windows (x86, 64-bit)&lt;/strong&gt; ZIP archive (choose the architecture that matches your Python installation -- if you're using 64-bit Python, get the 64-bit connector).&lt;/li&gt;
&lt;li&gt;Extract the ZIP and find &lt;code&gt;libmysql.dll&lt;/code&gt; in the &lt;code&gt;lib&lt;/code&gt; directory.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Architecture matters here.&lt;/strong&gt; If your Python installation is 64-bit, you need the 64-bit &lt;code&gt;libmysql.dll&lt;/code&gt;. Mixing 32-bit and 64-bit will cause the same "driver not loaded" error. You can check your Python architecture by running:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import struct
print(struct.calcsize("P") * 8, "bit")
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 id="placing-the-dll-where-qt-can-find-it"&gt;Placing the DLL Where Qt Can Find It&lt;/h2&gt;
&lt;p&gt;Once you have &lt;code&gt;libmysql.dll&lt;/code&gt;, you need to put it somewhere that Windows can find it when Qt tries to load the MySQL plugin. There are a few options.&lt;/p&gt;
&lt;h3&gt;Option A: Copy It Next to Your Python Executable (Recommended)&lt;/h3&gt;
&lt;p&gt;The simplest approach is to copy &lt;code&gt;libmysql.dll&lt;/code&gt; into the same directory as your &lt;code&gt;python.exe&lt;/code&gt;. If you're using a virtual environment, that's the &lt;code&gt;Scripts&lt;/code&gt; folder of your venv. If you're using a system Python installation, it's wherever &lt;code&gt;python.exe&lt;/code&gt; lives.&lt;/p&gt;
&lt;p&gt;Find your Python executable location with:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import sys
print(sys.executable)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Copy &lt;code&gt;libmysql.dll&lt;/code&gt; into that directory.&lt;/p&gt;
&lt;h3&gt;Option B: Copy It into the SQL Drivers Plugin Directory&lt;/h3&gt;
&lt;p&gt;You can also place &lt;code&gt;libmysql.dll&lt;/code&gt; directly into the &lt;code&gt;sqldrivers&lt;/code&gt; directory alongside &lt;code&gt;qsqlmysql.dll&lt;/code&gt;. Use the path you found earlier with &lt;code&gt;QLibraryInfo&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Option C: Add the MySQL lib Directory to Your PATH&lt;/h3&gt;
&lt;p&gt;Instead of copying files around, you can add the directory containing &lt;code&gt;libmysql.dll&lt;/code&gt; to your Windows &lt;code&gt;PATH&lt;/code&gt; environment variable. For example, if &lt;code&gt;libmysql.dll&lt;/code&gt; is in &lt;code&gt;C:\Program Files\MySQL\MySQL Server 8.0\lib\&lt;/code&gt;, add that path to your system or user &lt;code&gt;PATH&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After modifying &lt;code&gt;PATH&lt;/code&gt;, restart your terminal or IDE for the change to take effect.&lt;/p&gt;
&lt;h2 id="additional-dependencies-openssl"&gt;Additional Dependencies: OpenSSL&lt;/h2&gt;
&lt;p&gt;MySQL 8 uses encrypted connections by default, and &lt;code&gt;libmysql.dll&lt;/code&gt; from MySQL 8 may also depend on OpenSSL libraries (&lt;code&gt;libssl-1_1-x64.dll&lt;/code&gt; and &lt;code&gt;libcrypto-1_1-x64.dll&lt;/code&gt;, or similar names depending on your MySQL version). If you place &lt;code&gt;libmysql.dll&lt;/code&gt; and the connection still fails, these OpenSSL DLLs might also be missing.&lt;/p&gt;
&lt;p&gt;You can find them in the same MySQL installation directory (or connector download) as &lt;code&gt;libmysql.dll&lt;/code&gt;. Copy them to the same location where you placed &lt;code&gt;libmysql.dll&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id="testing-the-fix"&gt;Testing the Fix&lt;/h2&gt;
&lt;p&gt;Here's a short script you can use to verify that the MySQL driver loads correctly and can connect to your database:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtSql import QSqlDatabase

app = QApplication(sys.argv)

# Check available drivers
print("Available drivers:", QSqlDatabase.drivers())

# Try to load the MySQL driver
db = QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("localhost")
db.setDatabaseName("your_database_name")
db.setUserName("your_username")
db.setPassword("your_password")

if db.open():
    print("Connected successfully!")
    db.close()
else:
    print("Connection failed:", db.lastError().text())
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Replace the database name, username, and password with your own. If everything is set up correctly, you should see "Connected successfully!" printed to the console.&lt;/p&gt;
&lt;h2 id="a-complete-working-example"&gt;A Complete Working Example&lt;/h2&gt;
&lt;p&gt;Once the driver is loading properly, here's a complete example that connects to a MySQL database and displays a table in a &lt;code&gt;QTableView&lt;/code&gt;, similar to the original goal:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow,
    QTableView,
    QMessageBox,
)
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel


class MainWindow(QMainWindow):
    def __init__(self, db):
        super().__init__()
        self.setWindowTitle("MySQL Table Viewer")
        self.resize(600, 400)

        # Set up the model
        self.model = QSqlTableModel(self, db)
        self.model.setTable("your_table_name")  # Change this to your table
        self.model.select()

        # Set up the view
        table_view = QTableView()
        table_view.setModel(self.model)
        table_view.resizeColumnsToContents()

        self.setCentralWidget(table_view)


def main():
    app = QApplication(sys.argv)

    # Set up the database connection
    db = QSqlDatabase.addDatabase("QMYSQL")
    db.setHostName("localhost")
    db.setDatabaseName("your_database_name")
    db.setUserName("your_username")
    db.setPassword("your_password")

    if not db.open():
        QMessageBox.critical(
            None,
            "Database Error",
            f"Could not open database:\n{db.lastError().text()}",
        )
        return 1

    window = MainWindow(db)
    window.show()

    result = app.exec_()
    db.close()
    return result


if __name__ == "__main__":
    sys.exit(main())
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Replace &lt;code&gt;your_table_name&lt;/code&gt;, &lt;code&gt;your_database_name&lt;/code&gt;, &lt;code&gt;your_username&lt;/code&gt;, and &lt;code&gt;your_password&lt;/code&gt; with your actual values. If the MySQL driver is set up correctly, this will display the contents of your table in a window.&lt;/p&gt;
&lt;h2 id="troubleshooting-checklist"&gt;Troubleshooting Checklist&lt;/h2&gt;
&lt;p&gt;If you're still getting the error after following the steps above, work through this checklist:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Architecture mismatch&lt;/strong&gt;: Make sure your &lt;code&gt;libmysql.dll&lt;/code&gt; matches the bitness of your Python installation (both 64-bit or both 32-bit).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Missing OpenSSL DLLs&lt;/strong&gt;: MySQL 8's &lt;code&gt;libmysql.dll&lt;/code&gt; often requires OpenSSL libraries. Copy them from the same MySQL directory.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Wrong MySQL version&lt;/strong&gt;: Try to use a &lt;code&gt;libmysql.dll&lt;/code&gt; from a MySQL version close to the one your server is running.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Virtual environment&lt;/strong&gt;: If you're using a venv, make sure you're placing &lt;code&gt;libmysql.dll&lt;/code&gt; relative to the venv's Python executable, not your system Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Restart your terminal&lt;/strong&gt;: If you changed &lt;code&gt;PATH&lt;/code&gt;, your current terminal session won't see the update until you restart it.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="using-odbc-as-an-alternative"&gt;Using ODBC as an Alternative&lt;/h2&gt;
&lt;p&gt;If you continue to have trouble with the &lt;code&gt;QMYSQL&lt;/code&gt; driver, you can use the &lt;code&gt;QODBC&lt;/code&gt; driver instead as a workaround. Install the &lt;a href="https://dev.mysql.com/downloads/connector/odbc/"&gt;MySQL ODBC Connector&lt;/a&gt; and connect using an ODBC connection string:&lt;/p&gt;
&lt;div class="code-block"&gt;
&lt;span class="code-block-language code-block-python"&gt;python&lt;/span&gt;
&lt;pre&gt;&lt;code class="python"&gt;db = QSqlDatabase.addDatabase("QODBC")
db.setDatabaseName(
    "DRIVER={MySQL ODBC 8.0 Unicode Driver};"
    "SERVER=localhost;"
    "DATABASE=your_database_name;"
    "USER=your_username;"
    "PASSWORD=your_password;"
)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This sidesteps the &lt;code&gt;libmysql.dll&lt;/code&gt; issue entirely, since the ODBC driver manages its own MySQL connectivity. The ODBC driver name (e.g., &lt;code&gt;MySQL ODBC 8.0 Unicode Driver&lt;/code&gt;) must match what's installed on your system -- you can check the installed ODBC drivers in the Windows ODBC Data Source Administrator.&lt;/p&gt;
            &lt;p&gt;For an in-depth guide to building Python GUIs with PyQt5 see my book, &lt;a href="https://www.mfitzp.com/pyqt5-book/"&gt;Create GUI Applications with Python &amp; Qt5.&lt;/a&gt;&lt;/p&gt;
            </content><category term="pyqt5"/><category term="pyqt"/><category term="sql"/><category term="mysql"/><category term="windows"/><category term="python"/><category term="qt"/><category term="qt5"/><category term="databases"/></entry></feed>