【问题标题】:mysql.connector bug during "except" when compiled with pyinstaller?使用 pyinstaller 编译时“除外”期间的 mysql.connector 错误?
【发布时间】:2014-07-02 16:51:11
【问题描述】:

我有一个 python 程序,它可以调用我使用 pyinstaller 构建到 exe 中的 mysql 调用。 使用 pyinstaller 编译 --onefile 或 --onedir 会出现以下问题。

我已经能够使用 mysqldb 或 mysql.connector 成功连接并进行查询。

这里是mysqldb的连接逻辑:

# from http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python
try:
    db = MySQLdb.connect(host=hostname,user=username,passwd=password)
except MySQLdb.Error as e:
    reply = QtGui.QMessageBox.critical(self, "Error",str(e.args[1]))            
    return 

这里是mysql.connector的连接逻辑:

# http://dev.mysql.com/
try:
    db = mysql.connector.connect(host=hostname,user=username,password=password)
except mysql.connector.Error as e:   
    reply = QtGui.QMessageBox.critical(self, "Error",str(e.msg))
    return

如果我提供了错误的主机地址,那么在两个连接调用期间都会抛出“except”,并且错误消息会被捕获并显示。这适用于两个连接器之前我用 pyinstaller 编译。

但是,mysql.connector “except”不会出现在我的程序的编译版本中。 “except”确实对 mysqldb connect 调用正常工作并出现错误消息。

这使我得出结论 mysql.connector 有一个错误。其他人可以确认这一点还是我做错了什么?

【问题讨论】:

  • (删除了之前的评论)你能在 try/except 中添加一个“catch-all”来查看真正发生的异常吗?
  • 我尝试了一个通用的 except:,但在那种情况下也没有发现任何东西
  • 它只是简单地尝试,但从未发生过。令人惊讶的是,try/except 之后的代码也从未出现过。它似乎卡在了尝试中。
  • connection_timeout = 2 .. 也许解决起来很糟糕。
  • 但是我使用mysqldb和pyinstaller时没有卡住,作为脚本运行时也没有卡住mysql.connector或mysqldb。我确信我的代码没问题。只有2个可能的原因。 1. pyinstaller 无法正确处理 mysql.connector 或 2. mysql.connector 有错误。我不知道是哪个。

标签: python mysql mysql-python mysql-connector pyinstaller


【解决方案1】:

所有这些工具(pyinstaller、py2app、py2exe)都不支持try/except你想的那样。

生成的代码有(应该有)tryexcept 子句,但是为了推断程序实际需要哪些模块,只评估一个分支。

这可能会对动态加载造成各种破坏。

简单的hackish解决方法是直接包含所有相关的导入,例如:

import mysql
import mysql.connector
import mysql.secret_lazy_loaded_submodule
import QtGui
import QtGui.QMessageBox
import QtGui.secret_lazy_loaded_submodule
try:
    mysql.connector.foo()
except Exception:
    QtGui.QMessageBox.foo()

【讨论】:

  • 你如何推荐我去寻找必要的 secret_lazy_loaded_submodule?
  • 好的...我正在使用 pkgutil.walk_packages 查找所有子模块...然后我将尝试导入每个子模块直到它工作...
  • 就是这样!在这种情况下,我必须导入的 secret_lazy_loaded_submodule 是 mysql.connector.locales.eng.client_error。现在 try/except 在我的 python 程序的 pyinstaller 编译版本中成功捕获了错误。谢谢卡玛!!!
  • 天哪!对了谢谢!我们,连接器/Python 团队,必须进一步检查。但现在我们知道去哪里找了。
  • 你能解释一下你提到的那句话:“所有这些工具(pyinstaller,py2app,py2exe)都不支持try/except你想的方式。”这究竟是什么意思?我有一个与此相关的问题:stackoverflow.com/questions/70227633/…希望你能帮助我。谢谢。
【解决方案2】:

我添加这个答案是为了提高知名度,因为我的部分解决方案隐藏在评论中。

我遇到了同样的问题,错误的回溯是这样的:

Traceback (most recent call last):  
  File "main.pyw", line 239, in start  
  File "database.py", line 89, in connected  
  File "database.py", line 57, in __enter__  
  File "site-packages\mysql\connector\__init__.py", line 173, in connect  
  File "site-packages\mysql\connector\connection.py", line 102, in __init__  
  File "site-packages\mysql\connector\abstracts.py", line 731, in connect  
  File "site-packages\mysql\connector\connection.py", line 244, in _open_connection  
  File "site-packages\mysql\connector\network.py", line 518, in open_connection  
  File "site-packages\mysql\connector\errors.py", line 187, in __init__  
  File "site-packages\mysql\connector\locales\__init__.py", line 60, in get_client_error  
AttributeError: 'module' object has no attribute 'client_error'  

正如评论所说,导入是:

import mysql.connector.locales.eng.client_error

【讨论】:

    猜你喜欢
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 2020-09-13
    • 2021-12-12
    相关资源
    最近更新 更多