我有一个 Python 脚本,我想将其编译为 Windows 可执行文件。现在,
py2exe
在 Windows 上运行良好,但我希望能够在 Linux 上运行它。我的开发机器上确实有 Windows,但 Linux 是我的主要开发平台,我有点厌倦了只是为了创建 .exe
才重新启动到 Windows。我也不想购买第二个 Windows 许可证才能在 VirtualBox 等虚拟机中运行。有什么想法吗?PS:我知道
py2exe
并不完全编译 python 文件,而是使用 Python 解释器打包脚本。但无论哪种方式,结果都是您不需要安装 Python 来运行脚本。
1.5
以来,交叉编译功能已从 PyInstaller 中删除。在这里,展示如何使用
wine下的 PyInstaller 从 Python 脚本打包 Windows 可执行文件。 第1步:安装wine和Python
sudo apt-get install wine
wine msiexec /i python-2.7.10.msi /L*v log.txt
PS:
pip
(用于安装
pyinstaller
)。从这里下载Python安装包(例如
python-2.7.10.msi
)
brew cask install xquartz wine-stable
。
$ cd ~/.wine/drive_c/Python27
$ wine python.exe Scripts/pip.exe install pyinstaller
Successfully installed pyinstaller-3.1.1 pypiwin32-219
第三步:打包Python脚本
HelloWorld.py
打包 Python 脚本(例如
pyinstaller
)。$ 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
详细说明请参阅此处
,介绍如何使用 PyInstaller 执行此操作。总结如下:
如何使用 PyInstaller 和 WINE 在 Linux 上为 Python 创建 EXEwine python-3.8.9.exe
wine C:/Python38/python.exe -m pip install --upgrade pip
wine C:/Python38/python.exe -m pip install -r requirements.txt
requirements.txt
应包含 PyInstaller 本身wine C:/Python38/Scripts/pyinstaller.exe ...
选中“将 Python 3.8 添加到路径”
C:\\Python38
下载wine和python
sudo apt install wine
wine the-python-exe-you-downloaded
find ~/.wine -name pip.exe
这将为您提供点路径:
/home/yourusername/.wine/drive_c/users/yourusername/Local Settings/Application Data/Programs/Python/Python38/Scripts/pip.exe
安装pyinstaller
wine /home/yourusername/.wine/drive_c/users/yourusername/Local\ Settings/Application\ Data/Programs/Python/Python38/Scripts/pip.exe install pyinstaller
打包您的文件
find ~/.wine -name pyinstaller.exe
wine /home/yourusernmae/.wine/drive_c/users/yourusername/Local\ Settings/Application\ Data/Programs/Python/Python38/Scripts/pyinstaller.exe --onefile yourpythonfile