Python cx_freeze 设置脚本无法工作

2 投票
1 回答
653 浏览
提问于 2025-04-28 10:32

我有一个Python脚本想要打包成可执行文件。我使用了cx_freeze这个工具,并且运行了它。生成的.exe文件运行得很好,但在这个打包后的脚本中,我打开了一个.html文件。当文件打开时,浏览器给我报错:“文件未找到:Firefox无法在/c:/blah/blah/blah/somefile.html找到文件”。

我理解这是因为cx_freeze让我的操作系统在Linux和Windows之间搞混了。不过,我不太确定是不是这个原因,因为我在我的设置文件中有以下代码:

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

我的整个设置文件是:

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
   base = "Win32GUI"

setup(  name = "someexe",
    version = "0.1",
    description = "someexe description",
    options = {"build_exe": build_exe_options},
    executables = [Executable("someexe.py", base=base)])

cx_freeze的文档页面复制过来的,并根据我的需要进行了修改。

暂无标签

1 个回答

0

你应该用 os.chdir('C:\path\to\dir'),而不是 os.chdir('C:/path/to/dir')。这是你代码里的一个错误,不是你 cx_freeze 设置文件的问题。

撰写回答