python - 将 Linux 上的 Python 脚本打包成 Windows 可执行文件

标签 python linux windows py2exe

我有一个 Python 脚本,我想将它编译成 Windows 可执行文件。现在, py2exe 在 Windows 上运行良好,但我希望能够在 Linux 上运行它。我确实在我的开发机器上安装了 Windows,但 Linux 是我的主要开发平台,我有点厌倦了重新启动到 Windows 来创建 .exe。我也不想购买第二个 Windows 许可证才能在 VirtualBox 等虚拟机中运行。有什么想法吗?

PS:我知道 py2exe 并不像使用 Python 解释器打包脚本那样完全编译 python 文件。但无论哪种方式,结果都是您不需要安装 Python 来运行脚本。

最佳答案

正如其他回答者所说,交叉编译功能已从 PyInstaller 中删除。从 1.5 开始。在这里,展示如何使用 PyInstaller 从 Python 脚本打包 Windows 可执行文件。在 wine 下.

第 1 步:安装 wine 和 Python

sudo apt-get install wine
wine msiexec /i python-2.7.10.msi /L*v log.txt

PS:

  • 较新的 Python 版本已经包含 pip(用于安装 pyinstaller)。从here下载Python安装包(例如,python-2.7.10.msi)

  • 对于 macOS 用户,请使用 brew cask install xquartz wine-stable

第 2 步:在 wine 上安装 PyInstaller

$ cd ~/.wine/drive_c/Python27
$ wine python.exe Scripts/pip.exe install pyinstaller

Successfully installed pyinstaller-3.1.1 pypiwin32-219

第 3 步:打包 Python 脚本

使用 pyinstaller 打包 Python 脚本(例如 HelloWorld.py)。

$ wine ~/.wine/drive_c/Python27/Scripts/pyinstaller.exe --onefile HelloWorld.py

# filename: HelloWorld.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

print('Hello World!')

Windows 可执行文件位于 dist/

$ wine dist/HelloWorld.exe 
Hello World!
fixme:msvcrt:__clean_type_info_names_internal (0x1e24e5b8) stub

请参阅 here详细说明。

关于python - 将 Linux 上的 Python 脚本打包成 Windows 可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2950971/

相关文章:

python - 将整数数组转换为 "vector"

php - 通过 FTP 同时上传时文件无法运行

php - 是否可以在 PHP 中临时更改 PATH?

linux - 如何将实际变量传递到 bash 中的文本文件?

python - 格式化固定宽度的字符串(unicode 和 utf8)

c++ - 为什么在 Windows 启动时无法使用 fstream 创建/写入文件?

java - 在用户登录/注销时运行 Java 程序的批处理文件

python - 如何将多个参数传递给 Google App Engine python 中的处理程序?

python - 你如何表达一个没有参数的 Python Callable?

python - 快速迭代大型数据框中的行以确定列的内容