You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用py2exe转exe后Python程序错误处理失效问题求助

Fixing Custom Error Handling Failure After Py2exe Packaging with Python 3.4 & MySQL Connector

Looks like you've hit a common pitfall with py2exe and MySQL Connector's localization files! Here's what's going on and how to fix it:

Root Cause

When your database is offline, the MySQL Connector tries to load localization resources (like translated error messages) to generate meaningful errors. But py2exe doesn’t automatically include these locale files in the packaged EXE. Instead of throwing the expected mariadb.Error, it triggers an ImportError because it can’t find the required localization data—and your original except block only catches mariadb.Error, so the custom error never fires.

The cleanest fix is to tell py2exe to include the MySQL Connector’s locale directory. Update your setup.py to add these files as data assets:

from distutils.core import setup
import py2exe
import mysql.connector
import os

# Locate the MySQL Connector's locale folder
locale_path = os.path.join(os.path.dirname(mysql.connector.__file__), "locale")
# Collect all .po/.mo localization files
locale_files = [os.path.join(locale_path, f) for f in os.listdir(locale_path) if f.endswith((".po", ".mo"))]

setup(
    console=["your_script_name.py"],  # Replace with your actual script name
    data_files=[("mysql/connector/locale", locale_files)],
    options={"py2exe": {"bundle_files": 1, "compressed": True}}  # Adjust based on your needs
)

Re-run py2exe with this updated setup, and the localization files will be included in the EXE. Now when the database is offline, the Connector will find the locale files, throw the expected mariadb.Error, and your custom error handling will work as intended.

Solution 2: Expand Exception Handling (Quick Patch)

If you need a faster fix without re-packaging, modify your exception block to catch both mariadb.Error and the localization-related ImportError:

import mysql.connector as mariadb
try:
    mariadb_connection = mariadb.connect(user='root', password='', database='marsmenagerie')
    cursor = mariadb_connection.cursor()
except (mariadb.Error, ImportError) as e:
    # Check if it's the localization error we expect
    if isinstance(e, ImportError) and "localization support for language 'eng'" in str(e):
        # Treat it as a database connection failure
        clearscreen()
        print("Failed to connect to Database. (Error: 2475JWRT), Contact Censored")
        print("===========================")
        PAUSE()
        exit()
    # Handle other database errors normally
    clearscreen()
    print("Failed to connect to Database. (Error: 2475JWRT), Contact Censored")
    print("===========================")
    PAUSE()
    exit()

This will catch the unexpected ImportError and route it to your custom error flow. Note that this is a workaround—Solution 1 addresses the root problem by ensuring all required files are packaged.

内容的提问来源于stack exchange,提问作者That One

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:34:02