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

Pyinstaller打包的EXE无法加载mysql_native_password认证插件问题求助

Pyinstaller打包的EXE无法加载mysql_native_password认证插件问题求助

各位大佬,我遇到一个头疼的问题:我用Pyinstaller打包一个用到mysql.connector的Python应用,本地直接跑代码的时候连接数据库完全正常,但打包成EXE之后,一尝试连接就报错,错误信息如下:

Traceback (most recent call last):
  File "Report App.py", line 135, in run_report
  File "odbc_config.py", line 32, in run_query_tbred
  File "mysql\connector\pooling.py", line 322, in connect
  File "mysql\connector\connection_cext.py", line 151, in __init__
  File "mysql\connector\abstracts.py", line 1426, in connect
  File "mysql\connector\connection_cext.py", line 335, in _open_connection
_mysql_connector.MySQLInterfaceError: Authentication plugin 'mysql_native_password' cannot be loaded: The specified module could not be found.

我甚至还试了cx_freeze打包,结果也是一样的报错,只有原生Python运行的时候没问题。

我已经试过网上能找到的各种方法了,但都没用:

  • 我肯定已经用pip装了mysql-connector-python,毕竟原生运行正常
  • 手动在报错的代码文件里导入mysql_native_password模块,还打印了模块路径,能看到它在临时文件夹里存在:
    <module 'mysql.connector.plugins.mysql_native_password' from 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\_MEI260882\\mysql\\connector\\plugins\\mysql_native_password.pyc'>
    
    但就算这样,还是报找不到模块的错
  • 我在Pyinstaller的配置里加了mysql.connector的hidden import,但提示这个模块已经被导入了,没起到作用
  • 其他帖子里说的重装模块、手动导入之类的方法我都试了,全不管用

对了,我还手动改了mysql\connector\connection_cext.py里的代码,在连接前加了导入和打印:

try:
            from mysql.connector.plugins import mysql_native_password
            print(mysql_native_password)
            self._cmysql.connect(**cnx_kwargs)
            self._cmysql.converter_str_fallback = self._converter_str_fallback
            if self.converter:
                self.converter.str_fallback = self._converter_str_fallback
        except MySQLInterfaceError as err:
            raise get_mysql_exception(
                msg=err.msg, errno=err.errno, sqlstate=err.sqlstate
            ) from err

我的系统是Windows 11,连接的是远程数据库,实在不知道该怎么办了,有没有大佬遇到过类似的问题,求指点!

备注:内容来源于stack exchange,提问作者Gavin Jones

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 15:57:58