首页 > 解决方案 > 如何从包含 sys 模块的 python 文件中构建 exe 文件?

问题描述

我在 python 中制作了一个包含 sys 模块的程序。我想使用 cx_Freeze 将其转换为 exe 文件。所以我在 setup.py 中使用了以下代码:

import sys
from cx_Freeze import setup, Executable

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

options = {"build_exe": {"includes": "atexit"}}

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

setup(
    name="simple_PyQt5",
    version="0.1",
    description="Sample cx_Freeze PyQt5 script",
    options=options,
    executables=executables,
)

当我运行命令时: python3 setup.py build 它会在其中构建文件夹 build,其中有一个名为“exe.win-amd64-3.9”的文件夹,它有一个名为 lib、myfilename.exe、python3.dll 的文件夹和 exe.win-amd64 中的 python39.dll ...当我运行 myfilename.exe 时,它​​会打开和关闭,而我有一个 input() 它会询问我。所以你能帮我,我如何使用我想使用 cx_Freeze 转换为 exe 的文件中的 sys 模块

标签: pythoncx-freezesys

解决方案


**使用 cx_Freeze **

首先检查天气 cx_Freeze 库是否安装,如果没有安装使用 pip install cx_Freeze else 升级库使用

*pip install cx_Freeze --upgrade*

将代码依赖导入代码

创建标记为 Code.py 的代码文件

from datetime import datetime
import sys

print("Hello from cx_Freeze")
print(f"The current date is {datetime.today():%B %d, %Y %H:%M:%S}\n")

print(f"Executable: {sys.executable}")
print(f"Prefix: {sys.prefix}")
print(f"Default encoding: {sys.getdefaultencoding()}")
print(f"File system encoding: {sys.getfilesystemencoding()}\n")

print("ARGUMENTS:")
for a in sys.argv:
    print(f"{a}")
print()

print("PATH:")
for p in sys.path:
    print(f"{p}")
print()

**创建标记为run.py的运行文件**

from cx_Freeze import setup, Executable

executables = [Executable("hello.py")]

setup(
    name="hello",
    version="0.1",
    description="Sample cx_Freeze script",
    executables=executables,
)

根据您的方便更新代码。


推荐阅读