Python 个由 PyInstaller 自动捆绑的包

Python packages automatically bundled by PyInstaller

我在使用以下模块时编写了 Python 代码:

import tkinter as tk
from PIL import ImageGrab
from tkinter.colorchooser import *
from tkinter import filedialog

当我将应用程序与 Pyinstaller 捆绑在一起时,这些模块是否自动打包到 .exe 文件中?

您可能正在搜索 cx_freeze

就像@E.B的回答一样,cx_freeze是一个选择,但是如果它对你来说太复杂,你可以尝试Pyinstaller,这是一个更简单的基于命令行的工具.

Are these modules automatically packed into the .exe file when I bundle the application with Pyinstaller?

是的,他们是。只要它们是 packages supported by PyInstaller or part of the standard library 中的一员——有一些例外。

事实上,PyInstaller 文档(上面链接)是这样说的:

Most packages will work out of the box with PyInstaller.

This is especially true for pure Python packages and for packages not requiring additional files.

As for the packages in the Python standard library we only list the exceptions that don't work correctly; thus, if not listed, it works correctly.

作为标准库一部分的 Tkinter 列出,而 Pillow 库(PIL 包).所以这应该开箱即用。

最终,您必须尝试一下才能看到。受上面代码的启发,这里有一个 self-contained 示例,它采用 screen-shot,打开 Tkinter 提供的“另存为”对话框,让用户指定文件名,然后保存 screen-shot 使用 Pillow 作为 PNG 图像:

from PIL import ImageGrab
from tkinter import filedialog

screenshot = ImageGrab.grab()
filename = filedialog.asksaveasfilename()
screenshot.save(filename)

如果已安装 pyinstaller,可以在 (Windows) 命令提示符下使用以下命令将其捆绑为 .exe (Windows) 应用程序:

pyinstaller --windowed --onefile example.py

(在 Windows 10、Pillow 6.1.0、pyInstaller 3.5 上使用 Python 3.7.4(包括 Tkinter)进行测试。)