Search code examples
pythonmysqlpython-3.xcentos7pyside2

Missing MySql driver when using PySide2 QtSql QSqlDatabase


I'm porting some code from PySide to PySide2 and I've noticed that I'm missing a couple of sql drivers.

$ python3
Python 3.6.8 (default, Apr  2 2020, 13:34:55) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PySide.QtSql; import PySide2.QtSql
>>> PySide.QtSql.QSqlDatabase.drivers()
['QSQLITE', 'QSQLITE3', 'QMYSQL3', 'QMYSQL', 'QODBC3', 'QODBC', 'QPSQL7', 'QPSQL']
>>> PySide2.QtSql.QSqlDatabase.drivers()
['QSQLITE', 'QODBC', 'QODBC3', 'QPSQL', 'QPSQL7']

As you can see I'm missing QMYSQL on PySide2 (among others). I need that one for my application to work.

I've tried installing a couple of packages like mysql mysql-connector-python through pip but that didn't change anything. Then I tried changing QTDIR because I noticed it was pointing to /usr/lib64/qt-3.3 instead of /usr/lib64/qt5. That didn't do anything either.

I also checked /usr/lib64/qt4/plugins/sqldrivers and /usr/lib64/qt5/plugins/sqldrivers for libqsqlmysql.so and it's present in both folders.

I'm on CentOS 7 by the way. I'm trying to get the software on CentOS 7 and 8, though.

Pretty much all of the posts I've seen about it the drivers aren't missing but they can't be loaded.

Any idea what could be the problem?


Solution

  • Most likely, the plugin directory is not in "/usr/lib64/qt5/plugins" so it doesn't load the mysql plugin. The solution is to copy the plugin so the first thing is to know the PySide2 directory plugin by executing the following command:

    $ python3 -c "from PySide2 import QtCore; print(QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.PluginsPath))"
    

    Output:

    /usr/local/lib64/python3.6/site-packages/PySide2/Qt/plugins
    

    So you should copy the .so using the following command:

    $ cp /usr/lib64/qt5/plugins/sqldrivers/libqsqlmysql.so /usr/local/lib64/python3.6/site-packages/PySide2/Qt/plugins/sqldrivers
    

    Even so the .so points to the Qt of the OS instead of the Qt of PySide2:

    $ ldd  /usr/local/lib64/python3.6/site-packages/PySide2/Qt/plugins/sqldrivers/libqsqlmysql.so
    

    Output

    linux-vdso.so.1 =>  (0x00007fffb974f000)
    libQt5Sql.so.5 => /lib64/libQt5Sql.so.5 (0x00007faa76f00000)
    libQt5Core.so.5 => /lib64/libQt5Core.so.5 (0x00007faa76895000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007faa76679000)
    libmysqlclient.so.18 => /usr/lib64/mysql/libmysqlclient.so.18 (0x00007faa76179000)
    # ...
    

    for this you must change the rpath using patchelf:

    $ yum install epel-release
    $ yum install patchelf
    $ patchelf --set-rpath \$ORIGIN/../../lib  /usr/local/lib64/python3.6/site-packages/PySide2/Qt/plugins/sqldrivers/libqsqlmysql.so
    

    again:

    $ ldd  /usr/local/lib64/python3.6/site-packages/PySide2/Qt/plugins/sqldrivers/libqsqlmysql.so
    

    Output

    linux-vdso.so.1 =>  (0x00007ffd013ad000)
    libQt5Sql.so.5 => /usr/local/lib64/python3.6/site-packages/PySide2/Qt/plugins/sqldrivers/../../lib/libQt5Sql.so.5 (0x00007f6e1fb4c000)
    libQt5Core.so.5 => /usr/local/lib64/python3.6/site-packages/PySide2/Qt/plugins/sqldrivers/../../lib/libQt5Core.so.5 (0x00007f6e1f359000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f6e1f13d000)
    libmysqlclient.so.18 => /usr/lib64/mysql/libmysqlclient.so.18 (0x00007f6e1ec3d000)
    # ...
    

    Finally:

    $ python3 -c "from PySide2 import QtSql; print(QtSql.QSqlDatabase.drivers())"
    

    Output:

    ['QSQLITE', 'QMYSQL', 'QMYSQL3', 'QODBC', 'QODBC3', 'QPSQL', 'QPSQL7']
    

    It seems that there is no binary compatibility between the plugins so it must be compiled using the source code:

    $ python3 -m pip install aqtinstall
    $ python3 -m aqt install 5.15.0 linux desktop --outputdir qt
    $ sudo yum -y install git
    $ sudo yum -y install libxcb libxcb-devel xcb-util xcb-util-devel xcb-util-*-devel libX11-devel libXrender-devel libxkbcommon-devel libxkbcommon-x11-devel libXi-devel libdrm-devel libXcursor-devel libXcomposite-devel
    $ sudo yum -y install centos-release-scl
    $ sudo yum -y install devtoolset-7-gcc*
    $ sudo yum -y groupinstall 'Development Tools'
    $ sudo yum -y install mysql-devel
    $ scl enable devtoolset-7 bash
    $ git clone -b 5.15.0 git://code.qt.io/qt/qtbase.git
    $ cd qtbase/src/plugins/sqldrivers/mysql
    $ sed -i 's/QMAKE_USE += mysql/# QMAKE_USE += mysql/g' mysql.pro
    $ echo "INCLUDEPATH += /usr/include/mysql" >> mysql.pro
    $ echo "QMAKE_LIBDIR += /usr/lib64/mysql" >> mysql.pro
    $ echo "LIBS += -lmysqlclient" >> mysql.pro
    $ ../../../../../qt/5.15.0/gcc_64/bin/qmake
    $ make
    $ sudo cp ../plugins/sqldrivers/libqsqlmysql.so /usr/local/lib64/python3.6/site-packages/PySide2/Qt/plugins/sqldrivers
    

    Finally:

    $ python3 -c "from PySide2 import QtSql; print(QtSql.QSqlDatabase.drivers())"
    

    Output:

    ['QSQLITE', 'QMARIADB', 'QMYSQL', 'QMYSQL3', 'QODBC', 'QODBC3', 'QPSQL', 'QPSQL7']