如何使用 PyInstaller 从 PyCharm 项目创建可执行文件?

How do I create an executable file out of a PyCharm project using PyInstaller?

我在 PyCharm 中编写了一个项目,其中包含一个 .py 文件、一个 .txt 文件、一个 .ico 文件和 PyCharm 项目的常规 .idea 文件夹。全部保存在C:\Users\user\PycharmProjects\myproject.

我想使用 PyInstaller 创建一个单文件 .exe。但是当我 运行 命令 pyinstaller.exe --onefile --windowed myprogram.py 时,我得到以下错误:

'pyinstaller.exe' is not recognized as an internal or external command, operable program or batch file.

据我了解,这是因为 "pyinstaller.exe" 不在我 运行 命令提示符所在的位置。

但是,如果我 运行 cmd 在 pyinstaller 文件夹 (C:\Users\user\AppData\Local\Programs\Python\Python35-32\Scripts) 中,我的项目不存在。所以这也不管用。

我需要做什么才能将我的程序合并到一个 .exe 文件中?

运行 pyinstaller 来自您的项目目录,但将其称为 .exe 的完整目录,如 C:\PathTo\Pyinstaller.exe

所以你的命令看起来像

C:\Users\user\PycharmProjects\myproject> C:\PathTo\pyinstaller.exe --onefile --windowed myprogram.py

您可以将任一文件指定为绝对路径:

C:\Users\pemho\AppData\Local\Programs\Python\Python35-32\Scripts\pyinstaller.exe --onefile --windowed myprogram.py 应该在项目文件夹中工作,pyinstaller.exe --onefile --windowed C:\Users\user\PycharmProjects\myproject\myprogram.py 应该在 pyinstaller 文件夹中工作。

或者,您可以将 C:\Users\pemho\AppData\Local\Programs\Python\Python35-32\Scripts 添加到您的系统路径(参见 here)。