为通过cx_Freeze在Python中创建的可执行文件启动CMD控制台

0 投票
1 回答
4255 浏览
提问于 2025-04-18 09:56

我用Python创建了一个应用程序,并通过cx_Freeze把它变成了可执行文件。

在没有转换成可执行文件之前,它可以从cmd(在Windows系统中)接收输入。但是,当我把它变成exe文件后,它就不再提示我输入了。

我用以下代码作为我的脚本的setup.py

    includefiles = ["checkPointValueSheets.py"] # include any files here that you wish
includes = []
excludes = []
packages = ["lxml"]

exe = Executable(
 # what to build
   script = "app.py", # the name of your main python script goes here 
   initScript = None,
   base = None, # if creating a GUI instead of a console app, type "Win32GUI"
   targetName = "aflPredictionAutomation.exe", # this is the name of the executable file
   copyDependentFiles = True,
   compress = True,
   appendScriptToExe = True,
   appendScriptToLibrary = True,
   icon = None # if you want to use an icon file, specify the file name here
)

setup(
 # the actual setup & the definition of other misc. info
    name = "app", # program name
    version = "0.1",
    description = 'A general enhancement utility',
    author = "K Perkins",
    author_email = "",
    options = {"build_exe": {"excludes":excludes,"packages":packages,
      "include_files":includefiles}},
    executables = [exe]
)

请帮我在我点击exe文件时,立即启动cmd控制台。

当我运行这个可执行文件时,我遇到了这个错误。

在这里输入图片描述

谢谢

1 个回答

2

你代码里的注释已经说明了这一点(在 cx_Freeze的文档中也有),你只需要把那两行注释掉就可以了。

if sys.platform == "win32":
    base = "Win32GUI"

如果你把 base = None 这样写,你生成的exe文件就会是一个控制台应用程序(而不是图形界面应用程序)。如果这个程序不是从一个控制台启动的,Windows会自动为它提供一个新的控制台窗口。

撰写回答