How can i install and load the Qmysql driver using Pyside2 (pip) with python3.8? I've already tried to download git:qtbase and compiled the driver from there but I had any luck.
Unable to load QMYSQL Driver on PySide2
1.2k views Asked by nfnvsc AtThere are 2 answers
P.S. almost half of this info is officially published at https://doc.qt.io/qt-6/deployment-plugins.html
I use PySide6 on Windows 10 and %QT_SQL_DRIVER_PATH%
does not work for me.
What works for PySide6 is the %QT_PLUGIN_PATH%
environment variable.
Let's assume that you have built (or downloaded) a SQL driver for Qt platform into C:\projects\my-project-1\qt-plugins\sqldrivers
, so that there is qsqlmysql.dll
there.
CASE #1: you execute QSqlDatabase.addDatabase("QMYSQL")
, but you get the following error:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QPSQL
It means that qsqlmysql.dll
(a plugin for Qt platform) was not found.
The fix is to define the environment variable %QT_PLUGIN_PATH%
and put qsqlmysql.dll
into %QT_PLUGIN_PATH%/sqldrivers/
. You can even do it in your Python script before invocation of QSqlDatabase.addDatabase
:
os.environ['QT_PLUGIN_PATH'] = r'C:\projects\my-project-1\qt-plugins'
Note, qsqlmysql.dll
must be a release version built using MSVC (64-bit since it is year 2022), not MinGW. Debug version fails by some reason in my experience.
If you don't want to define the %QT_PLUGIN_PATH%
environment variable, an alternative fix is to copy qsqlmysql.dll
into one of these directories: <python-install-dir>/sqldrivers/
or <python-install-dir>/lib/site-packages/PySide6/plugins/sqldrivers/
.
Another alternative is to use QCoreApplication.addLibraryPath
like this:
app = QCoreApplication(sys.argv)
app.addLibraryPath(r'C:\projects\my-project-1\qt-plugins')
And yet another alternative is to create
<python-install-dir>\qt.conf
or <python-install-dir>\qt6.conf
with following content:
[Paths]
Prefix=C:/projects/my-project-1
Plugins=qt-plugins
or even so:
[Paths]
Plugins=C:/projects/my-project-1/qt-plugins
N.B. qt6.conf is ignored if qt.conf
doesn't exist. If you use qt6.conf
, you must create an empty qt.conf
at least.
CASE #2: you execute QSqlDatabase.addDatabase("QMYSQL")
, but you get the following error:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QMARIADB QMYSQL QSQLITE QODBC QPSQL
It means that qsqlmysql.dll
was found, but its dependencies were not.
The qsqlmysql.dll
additionally requires 3 DLLs: libmysql.dll
, libcrypto-1_1-x64.dll
, libssl-1_1-x64.dll
.
These DLLs must be located in %PATH%
or in current directory.
N.B. if you place the DLLs near your python script, this won't work if current working directory is different from scripts's location. But you can modify %PATH%
even in your Python script.
For example, if you copied said DLLs into deps
sub-directory of your project:
os.environ["PATH"] += os.pathsep + os.path.join(os.path.dirname(__file__), 'deps')
Another possible issues might be:
- architecture mismatch (some DLLs are 64-bit, while others are 32-bit).
- binary incompatibility - e.g. if "sqldrivers/qsqlmysql.dll" is built with MinGW or it's a debug version downloaded from https://github.com/thecodemonkey86/
Define the environment variable: set QT_DEBUG_PLUGINS=1
- this might help to troubleshoot such issues.
This answer covers not only the installation for Linux but for the other OS, besides that it also applies for pyqt5
The binaries used by Qt are the same ones used by PyQt5/PySide2 since they use the same base code so you will have to compile the plugins.
In this case, to compile the mysql plugin you must follow the official manual, which in summary is:
To find out the version of Qt with the version that the library was compiled with, the following can be used:
The above generates libqsqlmysql.so, qsqlmysql.dll or libqsqlmysql.dylib depending on the OS. That file must be pasted in the path:
To cover all the cases I have created a Github Actions that generates the binaries:
mysql_plugin.yml
The previous code generates the plugin that you can find here.
In the specific case of Ubuntu it can be reduced to:
sudo apt install libmysqlclient-dev
In the specific case of Windows it can be reduced to: