Python 程序在使用 py2exe 转换为 exe 后不处理错误
Python program doesn't handle error's after conversion to exe using 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 最大的问题。
我原以为它会引发我编写的错误。但是它只是引发了上面显示的错误。当 运行 数据库处于活动状态时,程序 运行 没问题。
嗯。我已经解决了!问题是标准 mysql 错误的 eng 本地化不会被 py2exe 复制,至少在这种情况下不会。您可以在错误的这一行找到证据:
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/
现在一切如梦如幻。
此问题在 中有答案。我在这里再次提到它以防 link 不起作用。该问题与@That One 提到的相同,其中 py2exe 或 pyInstaller 不复制模块文件 mysql.connector.locales.eng 到 exe。为此,我们可以只放代码部分,
from mysql.connector.locales.eng import client_error
在我们的 python 脚本中。这样,py2exe 或 pyInstaller 会将丢失的文件复制到 exe.
我已经为 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 最大的问题。
我原以为它会引发我编写的错误。但是它只是引发了上面显示的错误。当 运行 数据库处于活动状态时,程序 运行 没问题。
嗯。我已经解决了!问题是标准 mysql 错误的 eng 本地化不会被 py2exe 复制,至少在这种情况下不会。您可以在错误的这一行找到证据:
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/
现在一切如梦如幻。
此问题在
from mysql.connector.locales.eng import client_error
在我们的 python 脚本中。这样,py2exe 或 pyInstaller 会将丢失的文件复制到 exe.