将 Linux 上的 Python 脚本交叉编译为 Windows 可执行文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2950971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cross-compiling a Python script on Linux into a Windows executable
提问by Chinmay Kanchi
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 dohave 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?
我有一个 Python 脚本,我想将其编译为 Windows 可执行文件。现在,py2exe
在 Windows 上运行良好,但我希望能够从 Linux 运行它。我的开发机器上确实有 Windows,但 Linux 是我的主要开发平台,我有点厌倦重新启动到 Windows 只是为了创建.exe
. 我也不想购买第二个 Windows 许可证才能在 VirtualBox 等虚拟机中运行。有任何想法吗?
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.
PS:我知道py2exe
并没有像使用 Python 解释器打包脚本那样完全编译 python 文件。但无论哪种方式,结果都是您不需要安装 Python 来运行脚本。
采纳答案by luc
Did you look at PyInstaller?
你看过PyInstaller吗?
It seems that versions through 1.4 support cross-compilation (support was removed in 1.5+). See this answerfor how to do it with PyInstaller 1.5+ under Wine.
似乎到 1.4 的版本都支持交叉编译(在 1.5+ 中删除了支持)。请参阅此答案以了解如何在 Wine 下使用 PyInstaller 1.5+ 执行此操作。
Documentationsays:
文档说:
Add support for cross-compilation: PyInstaller is now able to build Windows executables when running under Linux. See documentation for more details.
添加对交叉编译的支持:PyInstaller 现在能够在 Linux 下运行时构建 Windows 可执行文件。有关更多详细信息,请参阅文档。
I didn't try it myself.
我自己没试过。
I hope it helps
我希望它有帮助
回答by Adam Crossland
You could run Windows in VirtualBoxin 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.
您可以在VirtualBox中运行 Windows以运行 py2exe。VBox 为自动化任务提供了强大的命令行客户端,因此您可以轻松地将其集成到您的开发过程中。
回答by Perkins
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.
我已经在 wine 中测试了 py2exe,它确实可以运行。你需要在 wine 中安装 python 才能工作,或者如果你只使用标准库,你可以将 py2exe 与 Windows 机器上的 py2exe 捆绑在一起,然后在 wine 中使用它。请记住,您需要在 wine 中使用与用于编译 python 相同版本的 ms 视觉 C 库,否则事情将无法正常工作。
回答by SparkAndShine
As mentioned by other answerers, the cross-compilation feature is removed from PyInstallersince 1.5
. Here, show how to package a Windows executable from Python scripts using PyInstallerunder wine.
正如其他回答者所提到的,交叉编译功能从PyInstaller 中删除,因为1.5
. 在这里,展示如何在wine下使用PyInstaller从 Python 脚本打包 Windows 可执行文件。
Step 1: Install wine and Python
第一步:安装wine和Python
sudo apt-get install wine
wine msiexec /i python-2.7.10.msi /L*v log.txt
PS: Newer Python versions already include pip
(is used to install pyinstaller
). Download Python installation package from here(e.g., python-2.7.10.msi
)
PS:较新的 Python 版本已经包含pip
(用于安装pyinstaller
)。从这里下载 Python 安装包(例如,python-2.7.10.msi
)
Step 2: Install PyInstaller on wine
第二步:在wine上安装PyInstaller
$ cd ~/.wine/drive_c/Python27
$ wine python.exe Scripts/pip.exe install pyinstaller
Successfully installed pyinstaller-3.1.1 pypiwin32-219
Step 3: Package Python scripts
第 3 步:打包 Python 脚本
Package Python scripts (e.g., HelloWorld.py
) with pyinstaller
.
将 Python 脚本(例如,HelloWorld.py
)与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!')
The Windows executable file is located in dist/
.
Windows 可执行文件位于dist/
.
$ wine dist/HelloWorld.exe
Hello World!
fixme:msvcrt:__clean_type_info_names_internal (0x1e24e5b8) stub
Refer to herefor the detailed description.
有关详细说明,请参阅此处。