mysql.connector bug during “except” when compiled with pyinstaller?

廉价感情. 提交于 2019-12-12 11:44:36

问题


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.

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

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 

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

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.

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.

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


回答1:


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

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



回答2:


I'm adding this answer for visibility, since part of my solution was tucked away in a comment.

I had the same problem, and the traceback for the error was something like this:

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'  

The import, like the comment said, is:

import mysql.connector.locales.eng.client_error



来源:https://stackoverflow.com/questions/23657934/mysql-connector-bug-during-except-when-compiled-with-pyinstaller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!