【问题标题】:Python, logging module not defined with cxfreezePython,未使用 cxfreeze 定义的日志记录模块
【发布时间】:2025-12-20 06:20:22
【问题描述】:

我想第一次使用 CxFreeze。我还想实现日志记录模块,它是 Python 3.6 中的标准模块。 我安装了最新版本的 CxFreeze(在 5.1.1 中)

这是我的代码!

from cx_Freeze import setup, Executable
import os,sys, logging

base = None
executables = [Executable("utt.py", base=base)]

packages = ["idna"]
includes = ["logging"]

options = {
    'build_exe': {
        'packages':packages,
        'includes':includes
    },
}

# On appelle la fonction setup
setup(
    name = "Import ",
    version = "1",
    description = "Gestion rapide",
    options = options,
    executables = executables
)

这里是错误信息

C:\Python\Python36\Scripts>C:\Python\Python36\Scripts\build\exe.win-amd64-3.6\utt.exe
2018-08-21 17:43:46.354712
2018-08-21-17-43-46
Traceback (most recent call last):
  File "C:\Python\Python36\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
    module.run()
  File "C:\Python\Python36\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
    exec(code, m.__dict__)
  File "utt.py", line 16, in <module>
    logging.basicConfig(filename=fileDate,level=logging.DEBUG,format='%(asctime)s %(levelname)s-8s %(message)s')
NameError: name 'logging' is not defined

拜托,我不明白为什么我的 mdoule 没有定义。我尝试了 Inclues 和 Packages 属性,甚至使用 include_files 属性导入日志库。

问候。

【问题讨论】:

  • 脚本不在可执行文件中时是否运行?这是 python 错误,不是 cxfreeze 错误。
  • 告诉我们utt.py。或者,更好的是,向我们展示minimal reproducible example,只需提供足以重现问题的代码即可。
  • 可能,您忘记在您的utt.py 中导入logging?另外我认为你不应该在included 中指定logging,因为它是一个标准模块,它总是被隐式包含。

标签: python logging module undefined


【解决方案1】:

确实,这不是 CXFreeze 错误。我在设置我的安装程序时犯了一个错误。我将导入命令移动到另一个 .py 文件(其中包含我的代码的配置参数)。我误用了import命令,认为嵌套导入更简单。

我应该在没有 cxfreeze 的情况下测试我的构建以进行此修改..

谢谢你们的cmets,是你们把我带错了。

【讨论】: