Python到一个文件中的EXE文件

Python to EXE file in one file

到目前为止,我已经使用 cx_freeze 将 .py 文件转换为 .exe 文件,但我得到了很多文件。有没有办法把它全部放到一个可执行文件中?

我看到 PyInstallerGUI 可以做到这一点,但它适用于 Python 2.7。 Python 3.4 也能做到吗?

我还没有尝试过,但是 PyInstaller 说 here 它可以做到这一点并且它支持 Python 2.7 和 Python 3.3+。

引用自链接页面:

PyInstaller can bundle your script and all its dependencies into a single executable named myscript (myscript.exe in Windows).

The advantage is that your users get something they understand, a single executable to launch. A disadvantage is that any related files such as a README must be distributed separately. Also, the single executable is a little slower to start up than the one-folder bundle.

Before you attempt to bundle to one file, make sure your app works correctly when bundled to one folder. It is is much easier to diagnose problems in one-folder mode.

PyInstaller 最高支持 Python 3.5。一旦你安装了它(在你的终端中输入 pip install pyinstaller),你可以在你的终端中做:

pyinstaller --onefile script.py

其中 script.py 是您要编译成 .exe 的脚本的名称

使用 --onefile 选项,它将只创建一个 .exe 文件。

我在 PyInstaller 文档中找到了这个:

pyinstaller --onefile  your-python-file.py

要查找更多信息:PyInstaller documentation