将 Linux 上的 Python 脚本打包为 Windows 可执行文件

发布于 2024-09-03 17:33:42 字数 333 浏览 6 评论 0 原文

我有一个 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦境 2024-09-10 17:33:42

正如其他回答者所提到的,自 1.5 以来,交叉编译功能已从 PyInstaller 中删除。此处,展示如何使用 PyInstaller 从 Python 脚本打包 Windows 可执行文件en.wikipedia.org/wiki/Wine_(软件)" rel="noreferrer">葡萄酒。

第1步:安装wine和Python

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

PS:

  • 较新的Python版本已经包含pip(用于安装pyinstaller)。从此处下载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

请参阅此处了解详细信息描述。

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

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)

  • For macos users, use brew cask install xquartz wine-stable.

Step 2: Install PyInstaller on wine

$ 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

Package Python scripts (e.g., HelloWorld.py) with 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/.

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

Refer to here for the detailed description.

堇年纸鸢 2024-09-10 17:33:42

您查看过 PyInstaller 吗?

似乎 1.4 之前的版本都支持交叉编译(1.5+ 中删除了支持)。请参阅此答案了解如何在 Wine 下使用 PyInstaller 1.5+ 执行此操作。

文档 说:

添加对交叉编译的支持:PyInstaller 现在能够在 Linux 下运行时构建 Windows 可执行文件。有关更多详细信息,请参阅文档。

我自己没有尝试过。

我希望它有帮助

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:

Add support for cross-compilation: PyInstaller is now able to build Windows executables when running under Linux. See documentation for more details.

I didn't try it myself.

I hope it helps

柳絮泡泡 2024-09-10 17:33:42

我写了一篇关于如何使用 PyInstaller 执行此操作的 博客文章。摘要如下:

  • 如何使用 PyInstaller 和 WINE 在 Linux 上为 Python 创建 EXE
    • 下载 Python 3.8 Windows 安装程序
    • 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:

  1. 选中“将 Python 3.8 添加到 PATH”
  2. 单击“自定义安装
  3. 单击“下一步”
  4. 单击“为所有用户安装”
  5. 将安装位置设置为 C:\\Python38
  6. 单击“安装”

随时阅读该帖子以获取更多详细信息。

I wrote a blog post on how to do this with PyInstaller. Here's the summary:

  • How to create EXEs for Python on Linux, using PyInstaller and WINE
    • Download Python 3.8 Windows installer
    • wine python-3.8.9.exe, then see instructions below
    • wine C:/Python38/python.exe -m pip install --upgrade pip
    • wine C:/Python38/python.exe -m pip install -r requirements.txt, and requirements.txt should include PyInstaller itself
    • wine C:/Python38/Scripts/pyinstaller.exe ...
    • All done!

Installing Python 3.8 in Wine:

  1. Check "Add Python 3.8 to PATH"
  2. Click "Customize installation
  3. Click "Next"
  4. Click "Install for all users"
  5. Set the install location as C:\\Python38
  6. Click "Install"
  7. Close the window.

Feel free to read the post to get more details.

遥远的绿洲 2024-09-10 17:33:42

您可以在 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.

无人问我粥可暖 2024-09-10 17:33:42

测试平台:Kubuntu 20.04、wine 6.0、python38

下载 wine 和 python

  1. https://www.python.org/downloads/release/python-3810/

  2. 安装wine
    sudo apt install wine

  3. 打开终端并运行
    wine the-python-exe-you-downloaded

  4. 运行
    查找 ~/.wine -n​​ame 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 -n​​ame 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

  1. Download windows version of python from https://www.python.org/downloads/release/python-3810/

  2. Install wine
    sudo apt install wine

  3. Open your terminal and run
    wine the-python-exe-you-downloaded

  4. 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

乖乖哒 2024-09-10 17:33:42

我已经在 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.

江湖正好 2024-09-10 17:33:42

我工作得很好,就像 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文