"except"期间的mysql.connector错误.用pyinstaller编译时? [英] mysql.connector bug during "except" when compiled with pyinstaller?

查看:124
本文介绍了"except"期间的mysql.connector错误.用pyinstaller编译时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python程序,该程序使用pyinstaller进行mysql调用,并将它们构建到exe中. 使用pyinstaller进行--onefile或--onedir编译时,会发生以下问题.

I have a python program which makes mysql calls that I build into an exe using pyinstaller. The following problem occurs with either a --onefile or a --onedir compile using pyinstaller.

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

I have been able to use either mysqldb or mysql.connector to successfully connect and make queries.

这是mysqldb连接逻辑:

Here is the mysqldb connect logic:

# 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连接逻辑:

Here is the mysql.connector connect logic:

# 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编译之前,之前这两个连接器均正常工作.

If I provide a bad host address then an "except" is thrown during both connect calls and the error message is trapped and displayed. This works correctly for both connectors before I compile with pyinstaller.

但是,mysql.connector"except" 不会出现在我程序的编译版本中.对于mysqldb connect调用,除外" 可以正常工作,并显示错误消息.

However, the mysql.connector "except" does not occur in the compiled version of my program. The "except" does work correctly for the mysqldb connect call and the error message appears.

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

This leads me to conclude that mysql.connector has a bug. Can anyone else confirm this or am I doing something wrong?

推荐答案

所有这些工具(pyinstaller,py2app,py2exe)都不支持您认为的try/except.

All these tools (pyinstaller,py2app,py2exe) do not support try/except the way you think.

生成的代码同时具有(应该具有)tryexcept子句,但是出于推断程序实际需要哪些模块的目的,仅评估一个分支.

The code generated has (well should have) both try and except clauses, but for the purposes of inferring which modules are actually needed by the program, only one branch is evaluated.

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

This may wreak all sorts of havoc with dynamic loading.

一个简单的修改方法是直接包含所有相关的导入,例如:

The simple, hackish fix is to include all the relevant imports directly, for example:

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()

这篇关于"except"期间的mysql.connector错误.用pyinstaller编译时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆