使用py2exe转换为exe后,Python程序无法处理错误

时间:2019-01-09 12:47:45

标签: python mysql python-3.4 py2exe

我已经为myqsl数据库编写了一个接口,该接口在出现错误的情况下(好像数据库处于脱机状态一样)会通过引发我的自定义错误来处理它,以便我知道出了什么问题。我对此感到满意,并决定准备将其转换为exe,并使用py2exe进行了此操作。这失败了,经过一番研究,我发现它不再起作用了,所以我降级到python 3.4。现在可以进行转换,但是转换后的程序不再处理错误。

我检查了我是否具有正确的mysql连接器,然后重新下载了相同的连接器,以便将其包含在内,因为我正在处理的错误通常是mysql.connector.Error。

原始程序中的错误处理:

import myqsl.connector as mariadb

try:
    mariadb_connection = mariadb.connect(user='root', password='', database='marsmenagerie')
    cursor = mariadb_connection.cursor()
except mariadb.Error:
    clearscreen()
    print("Failed to connect to Database. (Error: 2475JWRT), Contact Censored")
    print("===========================")
    PAUSE()
    exit()

如果应引发此错误,则转换后的程序会产生错误:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 525, in open_connection
    self.sock.connect(sockaddr)
ConnectionRefusedError: [WinError 10061] Kan geen verbinding maken omdat de doelcomputer de verbinding actief heeft geweigerd

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\locales\__init__.py", line 56, in get_client_error
globals(), locals(), ['client_error'])
ImportError: No module named 'mysql.connector.locales.eng'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 245, in _open_connection
    self._socket.open_connection()
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 528, in open_connection
    errno=2003, values=(self.get_address(), _strioerror(err)))
  File "C:\Python34\lib\site-packages\mysql\connector\errors.py", line 187, in __init__
    self.msg = get_client_error(self.errno)
  File "C:\Python34\lib\site-packages\mysql\connector\locales\__init__.py", line 59, in get_client_error
language))
ImportError: No localization support for language 'eng'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 148, in send_plain
    self.sock.sendall(packet)
OSError: [WinError 10057] Een aanvraag om gegevens te verzenden of te ontvangen is niet toegestaan omdat de socket niet is verbonden en omdat (tijdens het verzenden op een datagramsocket via een sendto-aanroep) geen adres is opgegeven

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\locales\__init__.py", line 56, in get_client_error
globals(), locals(), ['client_error'])
ImportError: No module named 'mysql.connector.locales.eng'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 102, in __init__
    self.connect(**kwargs)
  File "C:\Python34\lib\site-packages\mysql\connector\abstracts.py", line 731, in connect
    self._open_connection()
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 256, in _open_connection
    self.close()
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 276, in close
    self.cmd_quit()
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 617, in cmd_quit
    self._socket.send(packet, 0, 0)
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 151, in send_plain
    errno=2055, values=(self.get_address(), _strioerror(err)))
  File "C:\Python34\lib\site-packages\mysql\connector\errors.py", line 187, in __init__
    self.msg = get_client_error(self.errno)
  File "C:\Python34\lib\site-packages\mysql\connector\locales\__init__.py", line 59, in get_client_error
language))
ImportError: No localization support for language 'eng'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 148, in send_plain
    self.sock.sendall(packet)
OSError: [WinError 10057] Een aanvraag om gegevens te verzenden of te ontvangen is niet toegestaan omdat de socket niet is verbonden en omdat (tijdens het verzenden op een datagramsocket via een sendto-aanroep) geen adres is opgegeven

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\mysql\connector\locales\__init__.py", line 56, in get_client_error
globals(), locals(), ['client_error'])
ImportError: No module named 'mysql.connector.locales.eng'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "program.py", line 16, in <module>
  File "C:\Python34\lib\site-packages\mysql\connector\__init__.py", line 173, in connect
    return MySQLConnection(*args, **kwargs)
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 105, in __init__
    self.close()
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 276, in close
    self.cmd_quit()
  File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 617, in cmd_quit
    self._socket.send(packet, 0, 0)
  File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 151, in send_plain
    errno=2055, values=(self.get_address(), _strioerror(err)))
  File "C:\Python34\lib\site-packages\mysql\connector\errors.py", line 187, in __init__
    self.msg = get_client_error(self.errno)
  File "C:\Python34\lib\site-packages\mysql\connector\locales\__init__.py", line 59, in get_client_error
language))
ImportError: No localization support for language 'eng'    

我知道,它很长。我正在尝试使其尽可能短,但是我不想保留可能相关的信息。也许是我制作MCVE的最大问题。

我希望它会引起我编写的错误。但是,它只会引发上面显示的错误。在数据库处于活动状态下运行时,程序可以正常运行。

2 个答案:

答案 0 :(得分:0)

好吧。我已经解决了!问题是py2exe不会复制标准mysql错误的eng本地化,至少在这种情况下不会复制。您可以在以下错误的这一行中找到证据:

ImportError: No module named 'mysql.connector.locales.eng'

此问题以前似乎已经存在,因此已解决。 (ImportError: No localization support for language 'eng' in python)在此站点的帮助下,我手动将eng本地化添加到library.zip文件: http://pydoc.net/mysql-connector-python/2.0.2/

现在它像梦一样运作。

答案 1 :(得分:0)

此问题的答案为here。我在这里再次提到它,以防链接不起作用。此问题与@That One提到的问题相同,其中 py2exe pyInstaller 不会将模块文件 mysql.connector.locales.eng 复制到 exe 。为此,我们只需将代码部分放入

from mysql.connector.locales.eng import client_error

在我们的python脚本中。这样, py2exe pyInstaller 会将丢失的文件复制到 exe