[英]Unable to load QMYSQL Driver on PySide2
How can i install and load the Qmysql driver using Pyside2 (pip) with python3.8?如何使用 Pyside2 (pip) 和 python3.8 安装和加载 Qmysql 驱动程序? I've already tried to download git:qtbase and compiled the driver from there but I had any luck.
我已经尝试下载 git:qtbase 并从那里编译驱动程序,但我运气不错。
This answer covers not only the installation for Linux but for the other OS, besides that it also applies for pyqt5这个答案不仅涵盖了 Linux 的安装,还涵盖了其他操作系统,此外它还适用于 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. Qt 使用的二进制文件与 PyQt5/PySide2 使用的二进制文件相同,因为它们使用相同的基本代码,因此您必须编译插件。
In this case, to compile the mysql plugin you must follow the official manual , which in summary is:在这种情况下,要编译 mysql 插件,您必须遵循官方手册,总结为:
To find out the version of Qt with the version that the library was compiled with, the following can be used:要找出 Qt 的版本和编译库时使用的版本,可以使用以下命令:
python -c "from PyQt5.QtCore import QT_VERSION_STR; print('Qt version', QT_VERSION_STR)"
python -c "from PySide2.QtCore import qVersion; print('Qt version', qVersion())"
The above generates libqsqlmysql.so, qsqlmysql.dll or libqsqlmysql.dylib depending on the OS.以上根据操作系统生成 libqsqlmysql.so、qsqlmysql.dll 或 libqsqlmysql.dylib。 That file must be pasted in the path:
该文件必须粘贴在路径中:
python -c "import os; from PyQt5.QtCore import QLibraryInfo; print('QT_SQL_DRIVER_PATH', os.path.join(QLibraryInfo.location(QLibraryInfo.PrefixPath), 'plugins', 'sqldrivers'))"
python -c "import os; from PySide2.QtCore import QLibraryInfo; print('QT_SQL_DRIVER_PATH', os.path.join(QLibraryInfo.location(QLibraryInfo.PrefixPath), 'plugins', 'sqldrivers'))"
To cover all the cases I have created a Github Actions that generates the binaries:为了涵盖所有情况,我创建了一个生成二进制文件的 Github Actions:
mysql_plugin.yml mysql_plugin.yml
name: generate_mysql_plugin
on: [push]
jobs:
ci:
name: ${{ matrix.os.name }} Qt-${{ matrix.qt.qt_version }}
runs-on: ${{ matrix.os.runs-on }}
strategy:
fail-fast: false
matrix:
os:
- name: Windows
extension: "dll"
runs-on: windows-2019
- name: Linux
extension: "so"
runs-on: ubuntu-20.04
- name: MacOS
extension: "dylib"
runs-on: macos-10.15
qt:
- name: 5.15
qt_version: 5.15.0
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install Qt
uses: jurplel/install-qt-action@v2
with:
version: ${{ matrix.qt.qt_version }}
dir: ${{ github.workspace }}/qt/
- name: clone qtbase
run: git clone -b ${{ matrix.qt.qt_version }} https://code.qt.io/qt/qtbase.git
- name: Compile mysql plugin on Windows
if: matrix.os.name == 'Windows'
shell: cmd
run: |
choco install wget
wget https://downloads.mysql.com/archives/get/p/19/file/mysql-connector-c-6.1.11-winx64.zip
unzip mysql-connector-c-6.1.11-winx64.zip
copy /y "mysql-connector-c-6.1.11-winx64\lib\libmysql.dll" .
call "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Auxiliary/Build/vcvars64.bat"
cd qtbase/src/plugins/sqldrivers
qmake -- MYSQL_INCDIR="${{ github.workspace }}\mysql-connector-c-6.1.11-winx64\include" MYSQL_LIBDIR="${{ github.workspace }}\mysql-connector-c-6.1.11-winx64\lib"
nmake sub-mysql
nmake install
- name: Compile mysql plugin on Linux
if: matrix.os.name == 'Linux'
run: |
wget https://downloads.mysql.com/archives/get/p/19/file/mysql-connector-c-6.1.11-linux-glibc2.12-x86_64.tar.gz
tar zxvf mysql-connector-c-6.1.11-linux-glibc2.12-x86_64.tar.gz
sudo cp mysql-connector-c-6.1.11-linux-glibc2.12-x86_64/lib/*.so /usr/lib/x86_64-linux-gnu
sudo apt-get install freetds-dev
cd qtbase/src/plugins/sqldrivers
qmake
cd mysql
qmake
make
make install
- name: Compile mysql plugin on MacOS
if: matrix.os.name == 'MacOs'
run: |
brew install wget
wget https://cdn.mysql.com/archives/mysql-connector-c/mysql-connector-c-6.1.11-macos10.12-x86_64.tar.gz
tar zxvf mysql-connector-c-6.1.11-macos10.12-x86_64.tar.gz
sudo cp mysql-connector-c-6.1.11-macos10.12-x86_64/lib/libmysqlclient.dylib mysql-connector-c-6.1.11-macos10.12-x86_64/lib/libmysqlclient_r.dylib
sudo cp mysql-connector-c-6.1.11-macos10.12-x86_64/lib/libmysqlclient.18.dylib mysql-connector-c-6.1.11-macos10.12-x86_64/lib/libmysqlclient_r.18.dylib
sudo cp mysql-connector-c-6.1.11-macos10.12-x86_64/lib/*.dylib /usr/local/lib
cd qtbase/src/plugins/sqldrivers
qmake -- MYSQL_PREFIX="${{ github.workspace }}/mysql-connector-c-6.1.11-macos10.12-x86_64"
make sub-mysql
cd mysql
make install
- name: upload
uses: actions/upload-artifact@v2
with:
path: qtbase/src/plugins/sqldrivers/plugins/sqldrivers/*qsqlmysql.${{ matrix.os.extension }}
name: mysqlplugin-${{ matrix.os.name }}-Qt${{ matrix.qt.name }}
The previous code generates the plugin that you can find here .前面的代码生成了您可以在此处找到的插件。
In the specific case of Ubuntu it can be reduced to:在 Ubuntu 的特定情况下,它可以简化为:
sudo apt install libmysqlclient-dev
sudo apt install libmysqlclient-dev
In the specific case of Windows it can be reduced to:在 Windows 的特定情况下,它可以简化为:
PS almost half of this info is officially published at https://doc.qt.io/qt-6/deployment-plugins.html PS 几乎一半的信息在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.我在 Windows 10 上使用 PySide6,
%QT_SQL_DRIVER_PATH%
对我不起作用。 What works for PySide6 is the %QT_PLUGIN_PATH%
environment variable.适用于 PySide6 的是
%QT_PLUGIN_PATH%
环境变量。
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.假设您已经在
C:\projects\my-project-1\qt-plugins\sqldrivers
中构建(或下载)了 Qt 平台的 SQL 驱动程序,因此那里有qsqlmysql.dll
。
CASE #1: you execute QSqlDatabase.addDatabase("QMYSQL")
, but you get the following error:案例 #1:您执行
QSqlDatabase.addDatabase("QMYSQL")
,但您收到以下错误:
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QPSQL
It means that qsqlmysql.dll
(a plugin for Qt platform) was not found.这意味着没有找到
qsqlmysql.dll
(Qt 平台的插件)。
The fix is to define the environment variable %QT_PLUGIN_PATH%
and put qsqlmysql.dll
into %QT_PLUGIN_PATH%/sqldrivers/
.修复方法是定义环境变量
%QT_PLUGIN_PATH%
并将qsqlmysql.dll
放入%QT_PLUGIN_PATH%/sqldrivers/
。 You can even do it in your Python script before invocation of QSqlDatabase.addDatabase
:您甚至可以在调用
QSqlDatabase.addDatabase
之前在 Python 脚本中执行此操作:
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.注意,
qsqlmysql.dll
必须是使用 MSVC(64 位,因为它是 2022 年)而不是 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/
.如果您不想定义
%QT_PLUGIN_PATH%
环境变量,另一种解决方法是将qsqlmysql.dll
复制到以下目录之一: <python-install-dir>/sqldrivers/
或<python-install-dir>/lib/site-packages/PySide6/plugins/sqldrivers/
。
Another alternative is to use QCoreApplication.addLibraryPath
like this:另一种选择是像这样使用
QCoreApplication.addLibraryPath
:
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:还有另一种选择是创建
<python-install-dir>\qt.conf
或<python-install-dir>\qt6.conf
具有以下内容:
[Paths]
Prefix=C:/projects/my-project-1
Plugins=qt-plugins
or even so:甚至这样:
[Paths]
Plugins=C:/projects/my-project-1/qt-plugins
NB qt6.conf is ignored if qt.conf
doesn't exist.注意 如果qt.conf不存在,则忽略
qt.conf
。 If you use qt6.conf
, you must create an empty qt.conf
at least.如果使用
qt6.conf
,则至少必须创建一个空的qt.conf
。
CASE #2: you execute QSqlDatabase.addDatabase("QMYSQL")
, but you get the following error:案例#2:您执行
QSqlDatabase.addDatabase("QMYSQL")
,但您收到以下错误:
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 .这意味着找到了
qsqlmysql.dll
,但没有找到它的依赖项。 The qsqlmysql.dll
additionally requires 3 DLLs: libmysql.dll
, libcrypto-1_1-x64.dll
, libssl-1_1-x64.dll
. qsqlmysql.dll
还需要 3 个 DLL: libmysql.dll
、 libcrypto-1_1-x64.dll
、 libssl-1_1-x64.dll
。 These DLLs must be located in %PATH%
or in current directory.这些 DLL 必须位于
%PATH%
或当前目录中。
NB if you place the DLLs near your python script, this won't work if current working directory is different from scripts's location.注意,如果您将 DLL 放在您的 python 脚本附近,如果当前工作目录与脚本的位置不同,这将不起作用。 But you can modify
%PATH%
even in your Python script.但是您甚至可以在 Python 脚本中修改
%PATH%
。 For example, if you copied said DLLs into deps
sub-directory of your project:例如,如果您将上述 DLL 复制到项目的
deps
子目录中:
os.environ["PATH"] += os.pathsep + os.path.join(os.path.dirname(__file__), 'deps')
Another possible issues might be:另一个可能的问题可能是:
Define the environment variable: set QT_DEBUG_PLUGINS=1
- this might help to troubleshoot such issues.定义环境变量:
set QT_DEBUG_PLUGINS=1
- 这可能有助于解决此类问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.