将 python 编译成单个 .exe,为什么这么难?
Compiling python to a single .exe, why is this so difficult?
我对编程还很陌生。我最近使用 python 和 pygame 制作了一个简单的贪吃蛇游戏。当然,我可以通过 运行 运行 .py 脚本轻松玩我的游戏,但是为了与我不太懂计算机的朋友分享它,我继续尝试弄清楚如何将我的代码编译成一个单一的.exe 让他们更轻松。
我试过 py2exe 和 cx_Freeze,它们都给我一个带有 .exe 和其他几个文件(依赖项)的 dist 或 build 目录。这确实有效,因为我能够压缩整个文件夹并通过 .zip 分发,但这并不 理想 。我希望能够将其全部打包在一个文件中。
所以我开始谷歌搜索 'how to compile python into a single exe'。然而,与我以前尝试使用谷歌搜索的许多编程问题不同,我无法得到直接、可靠的答案。到目前为止,我尝试过的最新可靠答案给了我这个 setup.py 脚本:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
windows = ['snek.py'],
zipfile = None
)
# Python 3.4.4, py2exe 0.9.2.2; modules used: pygame, random
不过,它给了我一个完整的文件夹,而不是一个 .exe,程序不能 运行 没有它。
所以,我来找你,无所不知,伟大的 Whosebug,有两个问题。
以防万一我遗漏了什么:你如何建议我实现我的单一 .exe 梦想?
还有:这到底为什么这么难?
这里已经回答了这个问题:
py2exe - generate single executable file
PyInstaller will create a single .exe file with no dependencies; use the --onefileoption. It does this by packing all the needed shared libs into the executable, and unpacking them before it runs, just as you describe (EDIT: py2exe also has this feature, see minty's answer)
我对编程还很陌生。我最近使用 python 和 pygame 制作了一个简单的贪吃蛇游戏。当然,我可以通过 运行 运行 .py 脚本轻松玩我的游戏,但是为了与我不太懂计算机的朋友分享它,我继续尝试弄清楚如何将我的代码编译成一个单一的.exe 让他们更轻松。
我试过 py2exe 和 cx_Freeze,它们都给我一个带有 .exe 和其他几个文件(依赖项)的 dist 或 build 目录。这确实有效,因为我能够压缩整个文件夹并通过 .zip 分发,但这并不 理想 。我希望能够将其全部打包在一个文件中。
所以我开始谷歌搜索 'how to compile python into a single exe'。然而,与我以前尝试使用谷歌搜索的许多编程问题不同,我无法得到直接、可靠的答案。到目前为止,我尝试过的最新可靠答案给了我这个 setup.py 脚本:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
windows = ['snek.py'],
zipfile = None
)
# Python 3.4.4, py2exe 0.9.2.2; modules used: pygame, random
不过,它给了我一个完整的文件夹,而不是一个 .exe,程序不能 运行 没有它。
所以,我来找你,无所不知,伟大的 Whosebug,有两个问题。
以防万一我遗漏了什么:你如何建议我实现我的单一 .exe 梦想?
还有:这到底为什么这么难?
这里已经回答了这个问题: py2exe - generate single executable file
PyInstaller will create a single .exe file with no dependencies; use the --onefileoption. It does this by packing all the needed shared libs into the executable, and unpacking them before it runs, just as you describe (EDIT: py2exe also has this feature, see minty's answer)