我有一个 Python 脚本,我想将其编译为 Windows 可执行文件。现在,py2exe
在 Windows 上运行良好,但我希望能够在 Linux 上运行它。我的开发机器上确实安装了 Windows,但 Linux 是我的主要开发平台,而且我已经厌倦了仅仅为了创建 .exe
而重新启动到 Windows。我也不想购买第二个 Windows 许可证才能在 VirtualBox 等虚拟机中运行。有什么想法吗?
PS:我知道 py2exe 并不完全编译 python 文件,而是使用 Python 解释器打包脚本。但无论哪种方式,结果都是您不需要安装 Python 来运行脚本。
I have a Python script that I'd like to compile into a Windows executable. Now, py2exe
works fine from Windows, but I'd like to be able to run this from Linux. I do have Windows on my development machine, but Linux is my primary dev platform and I'm getting kind of sick of rebooting into Windows just to create the .exe
. Nor do I want to have to buy a second Windows license to run in a virtual machine such as VirtualBox. Any ideas?
PS: I am aware that py2exe
doesn't exactly compile the python file as much as package your script with the Python interpreter. But either way, the result is that you don't need Python installed to run the script.
发布评论
评论(7)
正如其他回答者所提到的,自
1.5
以来,交叉编译功能已从 PyInstaller 中删除。此处,展示如何使用 PyInstaller 从 Python 脚本打包 Windows 可执行文件en.wikipedia.org/wiki/Wine_(软件)" rel="noreferrer">葡萄酒。第1步:安装wine和Python
PS:
较新的Python版本已经包含
pip
(用于安装pyinstaller
)。从此处下载Python安装包(例如python-2.7.10.msi
)对于 Macos 用户,请使用
brew cask install xquartz wine-stable
。步骤2:在wine 上安装PyInstaller
步骤3:打包Python 脚本
使用
pyinstaller
打包Python 脚本(例如HelloWorld.py
)。Windows 可执行文件位于
dist/
中。请参阅此处了解详细信息描述。
As mentioned by other answerers, the cross-compilation feature is removed from PyInstaller since
1.5
. Here, show how to package a Windows executable from Python scripts using PyInstaller under wine.Step 1: Install wine and Python
PS:
Newer Python versions already include
pip
(is used to installpyinstaller
). Download Python installation package from here (e.g.,python-2.7.10.msi
)For macos users, use
brew cask install xquartz wine-stable
.Step 2: Install PyInstaller on wine
Step 3: Package Python scripts
Package Python scripts (e.g.,
HelloWorld.py
) withpyinstaller
.The Windows executable file is located in
dist/
.Refer to here for the detailed description.
您查看过 PyInstaller 吗?
似乎 1.4 之前的版本都支持交叉编译(1.5+ 中删除了支持)。请参阅此答案了解如何在 Wine 下使用 PyInstaller 1.5+ 执行此操作。
文档 说:
我自己没有尝试过。
我希望它有帮助
Did you look at PyInstaller?
It seems that versions through 1.4 support cross-compilation (support was removed in 1.5+). See this answer for how to do it with PyInstaller 1.5+ under Wine.
Documentation says:
I didn't try it myself.
I hope it helps
我写了一篇关于如何使用 PyInstaller 执行此操作的 博客文章。摘要如下:
wine python-3.8.9.exe
,然后参见下面的说明wine C:/Python38/python.exe -m pip install --upgrade pip
wine C:/Python38/python.exe -m pip install -rrequirements.txt
,并且requirements.txt
应包含 PyInstaller 本身wine C:/Python38/Scripts/pyinstaller.exe ...
在 Wine 中安装 Python 3.8:
C:\\Python38
随时阅读该帖子以获取更多详细信息。
I wrote a blog post on how to do this with PyInstaller. Here's the summary:
wine python-3.8.9.exe
, then see instructions belowwine C:/Python38/python.exe -m pip install --upgrade pip
wine C:/Python38/python.exe -m pip install -r requirements.txt
, andrequirements.txt
should include PyInstaller itselfwine C:/Python38/Scripts/pyinstaller.exe ...
Installing Python 3.8 in Wine:
C:\\Python38
Feel free to read the post to get more details.
您可以在 VirtualBox 中运行 Windows 来运行 py2exe。 VBox 提供了一个强大的命令行客户端来自动执行任务,因此您可以轻松地将其集成到您的开发过程中。
You could run Windows in VirtualBox in order to run py2exe. VBox offers a powerful command-line client for automating tasks, so it something that you could likely integrate into your development process with ease.
测试平台:Kubuntu 20.04、wine 6.0、python38
下载 wine 和 python
从 https://www.python.org/downloads/release/python-3810/
安装wine
sudo apt install wine
打开终端并运行
wine the-python-exe-you-downloaded
运行
查找 ~/.wine -name pip.exe
这将为您提供 pip 路径:
/home/yourusername/.wine/drive_c/users/yourusername/Local Settings/Application Data/Programs/Python/Python38/Scripts/pip.exe
Install pyinstaller
Run < code>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
Tested on Platform: Kubuntu 20.04, wine 6.0, python38
Download wine and python
Download windows version of python from https://www.python.org/downloads/release/python-3810/
Install wine
sudo apt install wine
Open your terminal and run
wine the-python-exe-you-downloaded
Run
find ~/.wine -name pip.exe
this will give you the pip path:
/home/yourusername/.wine/drive_c/users/yourusername/Local Settings/Application Data/Programs/Python/Python38/Scripts/pip.exe
Install pyinstaller
Run
wine /home/yourusername/.wine/drive_c/users/yourusername/Local\ Settings/Application\ Data/Programs/Python/Python38/Scripts/pip.exe install pyinstaller
Package your file
Find installation path
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
我已经在 wine 中测试了 py2exe,它确实有效。你需要在 wine 中安装 python 才能工作,或者如果你只使用标准库,你可以将 py2exe 与 Windows 机器上的 py2exe 捆绑在一起,然后在 wine 中使用它。请记住,您需要 wine 中的 ms Visual C 库版本与用于编译 python 的版本相同,否则将无法正常工作。
I have tested py2exe inside of wine, and it does function. You'll need to install python in wine for it to work, or if you only use the standard libarary, you can bundle py2exe with py2exe from the windows machine and then use it in wine. Just keep in mind you need the same version of the ms visual C libraries in wine as were used to compile python or things won't work properly.
我工作得很好,就像 kaptaan 上面写的那样:
https://stackoverflow.com/a/67857785/23262833
测试平台:Ubuntu 22.04、Wine 9.0、Python 3.12
下载wine 9.0 来自 winehq 下载页面 - https://wiki.winehq.org/Ubuntu (ubuntu 存储库有可用葡萄酒6.0)。
您可以运行Pyinstaller来构建windows .exe应用程序;
您可以运行 Innosetup 来为您的 Windows 应用程序创建安装程序。
I works fine as kaptaan wrote above:
https://stackoverflow.com/a/67857785/23262833
Tested on platform: Ubuntu 22.04, Wine 9.0, Python 3.12
Download wine 9.0 from winehq download page - https://wiki.winehq.org/Ubuntu (ubuntu repositories has available wine 6.0).
You can run Pyinstaller to build windows .exe app;
You can run Innosetup to create an installer for your windows aplication.