python - 如何从包含 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 模块
解决方案
**使用 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,
)
根据您的方便更新代码。
推荐阅读
- python - 如何在熊猫中将列的数据类型从字符串转换为列表?
- javascript - 在 index.js 中重命名导出默认值
- python - 为什么要求我定义一个已经定义的变量?
- java - 在Java中,使用引导加载程序时是否可以知道一个类是否已经加载?
- wordpress - 隐藏管道 | 在重力形式输入输出
- android - 处理完 Firebase 动态链接事件后,如何清除它?
- python - 如何在heroku(discord bot)上存储JSON文件
- node.js - Vercel NodeJS 应用程序部署错误:找不到模块“./dist/cli.js”等
- c# - 如何使用 ICSharpCode.SharpZipLib.Zip 在 zip 文件中创建多个子文件夹----请忽略..这个问题已经解决
- java - 当我将 image_gallery_saver 添加到我的颤振项目时,我很难出错