我一直在尝试让我用 Python 编写的一个小应用程序在任何运行 Windows 的计算机上作为独立程序运行,因此我尝试同时使用 cx_freeze 和 py2exe 来实现。 Py2exe 工作得很好,但由于一些兼容性问题,我真的更喜欢使用 cx_freeze。
cx_freeze 的问题是,编译代码及其所有依赖项后,我无法更改可执行文件的名称(这对于 py2exe 来说是完全可行的)。
所以,假设我有一个简单的hello.py
脚本:
print ("Hello World! ")
raw_input ("Press any key to exit. \n")
和我的
cxfreeze_setup.py
,我直接从他们的网站复制用于调试目的,看起来像这样:
import sys
from cx_Freeze import setup, Executable
setup( name = "hello",
version = "0.1",
description = "My simple hello world!!",
executables = [Executable("hello.py")])
当我在命令提示符中构建独立调用
python cxfreeze_setup.py build
时,一切都按预期顺利进行,并且可执行文件及其依赖项在通常的
build
文件夹中创建。如果我不对创建的
hello.exe
进行任何名称更改并运行它,那么一切都会完美运行!但是,假设我将
hello.exe
更改为
hey.exe
。现在,当我尝试运行
hey.exe
时,出现以下错误:
Traceback (most recent call last):
File "c:\Python27\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26 in <module>
code = importer.get_code(moduleName)
zipimport.ZipImportError: can't find module 'hey__main__'
如果我将
.exe
名称更改为
hi.exe
,那么错误将保持完全相同,除了最后一行现在显示
can't find module 'hi__main__'
最后,我想知道,使用 cx_freeze,我是否被迫在编译后不更改可执行文件名称,如果不是这种情况,我必须按顺序对我的 hello.py
或
cxfreeze_setup.py
脚本进行哪些修改编译后自由修改可执行文件名称,我可以用 py2exe 完美做到这一点。预先感谢您的帮助。
--target-name=NAME
,引用自doc:
--目标名称=名称或者只是:要创建的文件的名称,而不是脚本的基本名称和基本二进制文件的扩展名
setup(name = "guifoo",
version = "0.1",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("guifoo.py", base=base, targetName="what_you_want.exe")])
cx_Freeze 生成的 exe 使用自己的名称来查找要运行的 Python 脚本。这样做的优点是您可以让多个 exe 共享一组库。缺点是你不能轻易地重命名前任。
如果您确实需要重命名 exe,请打开library.zip,然后将
hello__main__.pyc
重命名为
hey__main__.pyc
(第一位应与您的 exe 名称匹配)。
targetName=
不起作用。
setup(name = "guifoo",
version = "0.1",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("guifoo.py", base=base, targetName="what_you_want.exe")])enter code here
但是,target_name= 确实如此。
setup(name = "guifoo",
version = "0.1",
description = "My GUI application!",
options = {"build_exe": build_exe_options},
executables = [Executable("guifoo.py", base=base, target_name="what_you_want.exe")])