cx_freeze生成exe时出错

6 投票
3 回答
14552 浏览
提问于 2025-04-16 15:23

嘿,我对把Python脚本编译成exe文件还比较陌生。我正在使用cx_freeze来编译我的脚本,但一旦构建完成,我运行exe文件时就出现了这个错误。我在网上查了很多资料,但还是不太确定。错误信息是:

Cannot import traceback module.
Exception: No module named re
Original Exception: No module named re

我不太确定该怎么修复这个问题。我看到有说法可能是Python里有一个叫re的模块,而cx_freeze模块里也有一个同名的re模块,这可能导致了冲突?

我的设置文件看起来是这样的:

from cx_Freeze import setup, Executable

includes = []
includefiles = ['remindersText.pkl']
eggsacutibull = Executable(
    script = "podlancer.py",
    initScript = None,
    base = 'Win32GUI',
    targetName = "podlancer.exe",
    compress = True,
    copyDependentFiles = True,
    appendScriptToExe = False,
    appendScriptToLibrary = False,
    icon = None
    )

setup(
        name = "Podlancer",
        version = "0.1",
        author = 'jono',
        description = "Podlancer UI script",
        options = {"build_exe": {"includes":includes, "include_files": includefiles}},
        executables = [eggsacutibull]
        )

3 个回答

0

我遇到了同样的问题,把 re 放在 includes 里对我来说没用。这样做在重建 .py 文件时出现了 cx_Freeze.freezer.ConfigError 的错误。

import sys
from cx_Freeze import setup, Executable

build_exe_options = {'include_files': ['re']}

setup(  name = "Foreground Window Montior",
        version = "0.1",
        description = "Query the foreground window.",
        options = {'build_exe': build_exe_options},
        executables = [Executable("actWin_Query.py")])

如果我把 re 放在 packages 里,而不是放在 include_files 里,就没有出现这个编译错误。

import sys
from cx_Freeze import setup, Executable

build_exe_options = {"packages": ["re"]}

setup(  name = "Foreground Window Montior",
        version = "0.1",
        description = "Query the foreground window.",
        options = {'build_exe': build_exe_options},
        executables = [Executable("actWin_Query.py")])
2

如果运行程序的工作目录不是可执行文件所在的目录,cx_freeze 就会出错。

你第一个导入的模块是 re 吗?如果你换个顺序导入,会发生什么呢?

6

试着把

includes = []

改成

includes = ["re"]

这样对我有效

撰写回答