You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller打包exe后出现mysql_native_password认证插件不支持错误

解决PyInstaller打包exe后mysql_native_password认证插件不支持的问题

问题背景

Python代码使用mysql-connector-python连接MySQL,控制台运行正常,但通过PyInstaller打包为单文件exe后,抛出错误:

mysql.connector.errors.NotSupportedError: Authentication plugin 'mysql_native_password' is not supported

打包命令:

python -m PyInstaller --noconsole --onefile --icon=icon.ico --name=Gkod main.py

核心原因

PyInstaller在打包时仅收集代码中显式导入的模块。虽然连接配置指定了mysql_native_password作为认证插件,但代码中仅导入了caching_sha2_password,导致mysql_native_password相关模块未被打包进exe,运行时无法找到该插件。

解决方案

方案1:显式导入mysql_native_password插件

在代码开头添加以下导入语句,让PyInstaller识别并打包该模块:

from mysql.connector.plugins import mysql_native_password

现有代码已导入caching_sha2_password,补上这行后重新执行打包命令即可。

方案2:通过PyInstaller spec文件手动添加缺失模块

如果方案1无效,可通过spec文件强制指定隐藏导入:

  1. 生成spec文件:
python -m PyInstaller --noconsole --onefile --icon=icon.ico --name=Gkod main.py --specpath .
  1. 打开生成的Gkod.spec文件,找到hiddenimports参数,添加缺失模块:
a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=['mysql.connector.plugins.mysql_native_password', 'mysql.connector.locales.eng.client_error'],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
  1. 使用spec文件重新打包:
python -m PyInstaller Gkod.spec

方案3:移除auth_plugin手动指定(尝试自动协商)

若MySQL服务器支持自动协商认证方式,可移除连接配置中的auth_plugin参数,让mysql-connector-python自动匹配合适的认证插件:

config = {
    'user': '-',
    'password': "-",
    'host': '-',
    'database': '-',
    'charset': 'utf8'
    # 移除auth_plugin配置行
}

内容的提问来源于stack exchange,提问作者Yunus Emre KÖSE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 06:22:13