如何使用 pyinstaller 将包转换为 exe?

rde*_*ges 5 python windows pyinstaller software-packaging python-2.7

我有一个开源的 python 命令行程序,可以在 Python 2.7、Python3+ 上运行,并且是跨平台的。

我正在尝试更轻松地将它打包为我的 Windows 用户的可执行文件。这个包的源代码在 Github 上:https : //github.com/stormpath/stormpath-cli

我正在尝试使用pyinstaller打包我的 Python 程序,但遇到了问题。

我正在从 Windows 8 框中运行以下命令:

$ pyinstaller --onefile setup.py
Run Code Online (Sandbox Code Playgroud)

这成功地为我生成了一个 EXE 文件,但是当我去运行它时,我收到以下错误:

$ pyinstaller --onefile setup.py
Run Code Online (Sandbox Code Playgroud)

出于测试目的,为了帮助缩小问题范围,我创建了一个test.py包含以下代码的脚本:

print('hello, world!')
Run Code Online (Sandbox Code Playgroud)

然后也打包成一个exe:

$ pyinstaller --onefile test.py
Run Code Online (Sandbox Code Playgroud)

当我运行这个生成的 exe 时,一切正常!Hello world 按预期输出。

我相信正在发生的事情是我没有告诉 pyinstaller 如何正确“检测”我的项目是一个 python 包,而不是单个文件脚本。

我已经阅读了很多文档,并在谷歌上搜索了很多,但还没有找到一种方法来指定一个包供 pyinstaller 分析。

我错过了什么?

Jon*_*art 5

虽然我认为这是一件完全合理的事情,但看起来 PyInstaller 根本不支持从包(带有__main__.py)构建应用程序。

请参阅https://github.com/pyinstaller/pyinstaller/issues/2560

作为解决方法,您可以编写一个小存根(在包外部),它与您的__main__.py. 然后将 PyInstaller 指向该位置。

  • 请注意,您可以简单地使用“__main__.py”作为 PyInstaller 的脚本,而不是添加另一个文件,并确保所有导入都是绝对的(而不是相对的)。这是 GitHub 上的[评论中提到的](https://github.com/pyinstaller/pyinstaller/issues/2560#issuecomment-377917879)。 (3认同)