如何使 Python 脚本独立可执行文件在没有任何依赖的情况下运行?

新手上路,请多包涵

我正在构建一个 Python 应用程序,不想强迫我的客户安装 Python 和模块。

那么,有没有办法将 Python 脚本编译为独立的可执行文件?

原文由 Jeff 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 325
2 个回答

You can use py2exe as already answered and use Cython to convert your key .py files in .pyc , C compiled files, like .dll in Windows and .so 在 Linux 上。

它比常见的 .pyo.pyc 文件更难还原(而且性能也有所提高!)。

原文由 neurino 发布,翻译遵循 CC BY-SA 4.0 许可协议

您可以使用 PyInstaller 将 Python 程序打包为独立的可执行文件。它适用于 Windows、Linux 和 Mac。

PyInstaller 快速入门

从 PyPI 安装 PyInstaller:

 pip install pyinstaller

转到程序目录并运行:

 pyinstaller yourprogram.py

这将在名为 dist 的子目录中生成包。

 pyinstaller -F yourprogram.py

添加-F(或–onefile)参数会将所有内容打包到单个“exe”中。

 pyinstaller -F --paths=<your_path>\Lib\site-packages  yourprogram.py

遇到“ImportError”你可能会考虑侧包。

  pip install pynput==1.6.8

仍在 Import-Erorr 中运行 - 尝试降级 pyinstaller - 请参阅 将 pynput 与 pyinstaller 一起使用时出现错误

有关更详细的演练,请参阅 手册

原文由 Rumple Stiltskin 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题