【问题标题】:How to build out an exe file out of a python file containing sys module?如何从包含 sys 模块的 python 文件中构建 exe 文件?
【发布时间】:2021-05-14 04:54:01
【问题描述】:

我在 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”的文件夹,在 exe.win-amd64 中有一个名为 lib、myfilename.exe、python3.dll 和 python39.dll 的文件夹...当我运行 myfilename.exe 时,它​​会打开和关闭,而我有一个 input() 它会询问我。 所以你能帮我看看如何使用我想使用 cx_Freeze 转换为 exe 的文件中的 sys 模块

【问题讨论】:

标签: python cx-freeze sys


【解决方案1】:

**使用 cx_Freeze **

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

*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,
)

根据您的方便更新代码。

【讨论】:

  • 谢谢,Athul A,但它不起作用。打开的窗口关闭得很快
  • 你用的是哪个操作系统。
  • @AthuR 尝试在@AthulA 的Code.py 示例末尾添加input("Press enter to exit") 以防止脚本立即关闭,请参阅How to keep a Python script output window open? 或从cmd 中运行您的可执行文件提示。
  • 所以正如你所说,我在脚本末尾添加了 input("Press enter to exit") 但是当我创建它的 exe 文件并运行它时,它只要求我输入("Press enter to exit") 的答案,它不会要求我输入 sys。
猜你喜欢
  • 2015-07-13
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-17
  • 2015-08-24
  • 1970-01-01
相关资源
最近更新 更多